Python 找不到带有post请求的Django页面
因此,我是Django的新手,我正在尝试创建一个HTML表单(只是在学习名称输入教程之后),我可以输入名称,但不能直接进入/Thanke.HTML页面Python 找不到带有post请求的Django页面,python,django,Python,Django,因此,我是Django的新手,我正在尝试创建一个HTML表单(只是在学习名称输入教程之后),我可以输入名称,但不能直接进入/Thanke.HTML页面 $ views.py from django.http import HttpResponseRedirect from django.shortcuts import render from .forms import NameForm def get_name(request): # if this is a POST reque
$ views.py
from django.http import HttpResponseRedirect
from django.shortcuts import render
from .forms import NameForm
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)
print(form)
# 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('/polls/thanks.html')
# if a GET (or any other method) we'll create a blank form
else:
form = NameForm()
return render(request, 'name.html', {'form': form})
当我进入页面时,我可以很好地输入我的名字,但当我提交时,我得到
Using the URLconf defined in mysite.urls, Django tried these URL patterns, in this order:
polls/ [name='index']
admin/
The current path, polls/thanks.html, didn't match any of these.
即使Thank.html在/polls中
抱歉,如果修复非常简单,我只是以前没有使用Django
谢谢:)在views.py中创建一个名为
谢谢的视图
def thanks(request):
return render(request, 'thanks.html')
现在,将/poll/thanky/
url链接到感谢
模板,方法是将路径('thanky/',views.thanking,name='thanky')
添加到你的polls应用程序的url.py
$ mysite/polls/urls.py
from django.urls import path
from polls import views
urlpatterns = [
path('thanks/', views.thanks, name='thanks'),
]
最后在get_name视图中更改以下行
return HttpResponseRedirect('/polls/thanks/')
更改主URL.py
:
url(r'^polls/', include('polls.urls')),
url(r'^$', views.get_name, name='index'),
在应用程序的url.py
中:
url(r'^polls/', include('polls.urls')),
url(r'^$', views.get_name, name='index'),
并在您的视图.py中更改为:
if form.is_valid():
# process the data in form.cleaned_data as required
# ...
# redirect to a new URL:
return render(request, 'thanks.html')
在name.html中,使用
if form.is_valid():
# process the data in form.cleaned_data as required
# ...
# redirect to a new URL:
return render(request, 'thanks.html')