Python Django表单处理
我正在上表单教程。我试图创建一个简单的表单元素,它将发布一些数据。这是在URL.py中启动的函数:Python Django表单处理,python,django,django-forms,Python,Django,Django Forms,我正在上表单教程。我试图创建一个简单的表单元素,它将发布一些数据。这是在URL.py中启动的函数: def get_name(request): # if this is a POST request we need to process the form data if request.method == 'POST': # create a form instance and populate it with data from the request: form = Nam
def get_name(request):
# if this is a POST request we need to process the form data
if request.method == 'POST':
# create a form instance and populate it with data from the request:
form = NameForm(request.POST)
# check whether it's valid:
if form.is_valid():
# process the data in form.cleaned_data as required
# ...
# redirect to a new URL:
return HttpResponseRedirect('/thanks/')
# if a GET (or any other method) we'll create a blank form
else:
form = NameForm()
return render(request, 'name.html', {'form': form})
HTML代码如下所示:
<form action="/your-name/" method="post">
{% csrf_token %}
{{ form }}
<input type="submit" value="Submit" />
</form>
{%csrf_令牌%}
{{form}}
当我向表单输入一些数据并单击“提交”按钮时,连接到/your name/URL的函数启动,我被重定向到/your name/。我想知道为什么连接到/Thank/URL的函数没有启动。如何访问它?要使视图工作,表单的操作
/your name/
必须由视图get\u name
提供
您尚未显示url模式,因此无法判断是否存在错误 你有拼写错误,我看不出或不明白你的问题。什么?您已经将表单设置为指向/your name/,因此当然这就是要调用的URL。如果你想调用/Thank/,你应该把它指向那个。好的,但是什么时候启动函数form.is\u valid()并执行HTTP重定向呢?