gpt4 book ai didi

python - 将表单传递到 HTML 页面 django

转载 作者:行者123 更新时间:2023-11-30 23:31:03 25 4
gpt4 key购买 nike

我目前正在学习 Django,我想知道我是否正确理解了这一切。在我的views.py中,我使用args={}创建了一个args字典。我使用 args.update(csrf(request))args['form'] = MyRegistrationForm() 将 2 个值传递到 args 中。我想知道在 return render(request, 'register.html', args) 行中,我是否将一个空表单从 args['form'] 传递到标记为 {{form}} 的 HTML 页面?

我的register.html:

{% extends 'base.html' %}

{% block content %}

<h2>Register</h2>
<form action = '/accounts/register/' method = 'POST'>{% csrf_token %}
{{form}}
<input type = 'submit' value = "Register" />
</form>

{% endblock %}

我的观点.py:

def register_user(request):
# second time around
if request.method == 'POST': # look into request.method and see if it has POST
form = MyRegistrationForm(request.POST) # pass through the values in the POST to the custom form to create form object
if form.is_valid(): # check if information if correct
form.save() # if correct, save form, and that will save the registration information for the new user
return HttpResponseRedirect('/accounts/register_success')

# first time around
args = {}
args.update(csrf(request))
args['form'] = MyRegistrationForm() # BLANK user creation form with no information
return render(request, 'register.html', args) # passes 'form' from args into the html page

最佳答案

am I passing in an empty form from args['form'] into the HTML page where it is labeled {{form}}

是的。

关于python - 将表单传递到 HTML 页面 django,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20194847/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com