所以我是 Django 新手,我正在嘗試創建一個 HTML 表單(只需按照名稱輸入教程進行操作),我可以輸入名稱,但無法直接進入 /thanks.html 頁面。$ views.pyfrom django.http import HttpResponseRedirectfrom django.shortcuts import renderfrom .forms import NameFormdef 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})$ name.html<html> <form action="/polls/thanks.html" method="post"> {% csrf_token %} {{ form }} <input type="submit" value="Submit"> </form><html>$ /mysite/urlsfrom django.contrib import adminfrom django.urls import include, pathurlpatterns = [ path('polls/', include('polls.urls')), path('admin/', admin.site.urls),]$ mysite/polls/urls.pyfrom django.urls import pathfrom polls import viewsurlpatterns = [ path('', views.get_name, name='index'),]當我進入該頁面時,我可以很好地輸入我的名字,但是當我提交時,我得到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.即使thanks.html位于/polls內抱歉,如果修復非常簡單,我只是以前沒有使用過 Django。
2 回答

Cats萌萌
TA貢獻1805條經驗 獲得超9個贊
創建一個視圖,thanks在views.py中調用
def thanks(request):
return render(request, 'thanks.html')
現在,通過添加到投票應用程序的 urls.py將/poll/thanks/URL 鏈接到模板。thankspath('thanks/', views.thanks, name='thanks')
$ 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/')

暮色呼如
TA貢獻1853條經驗 獲得超9個贊
改變你的主要urls.py
:
url(r'^polls/', include('polls.urls')),
在您的應用程序中urls.py
:
url(r'^$', views.get_name, name='index'),
并且在您views.py
更改為:
if form.is_valid(): # process the data in form.cleaned_data as required # ... # redirect to a new URL: return render(request, 'thanks.html')
添加回答
舉報
0/150
提交
取消