Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 找不到带有post请求的Django页面_Python_Django - Fatal编程技术网

Python 找不到带有post请求的Django页面

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

因此,我是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 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')