【发布时间】:2019-09-15 06:06:51
【问题描述】:
我查看了以前的答案(许多是针对旧版本的 Django 的,没有帮助),但无法在我的代码中发现错误。我在运行服务器并单击表单的“提交”按钮时收到“找不到页面”错误。
问题似乎是重定向。我不太了解错误消息(见下文)或它的工作原理,因此解释一下会有所帮助。
The current path, addMessage/worldguestbook/worldguestbook.html
相关的views.py代码和功能**
def addMessage(request):
new_item =GuestBookItem(content=request.POST['content'])
new_item.save()
return HttpResponseRedirect('worldguestbook/worldguestbook.html')
表单(在 html 中)
<form action="/addMessage/" method="post">{% csrf_token %}
<input type="text" name="content"/>
<input type="submit" value="Add"/>
</form>
urls.py
urlpatterns = [
path('admin/', admin.site.urls),
path('worldguestbook/',worldguestbookView),
path('login/',loginView),
path('addMessage/',addMessage),
]
错误 1
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/addMessage/worldguestbook/worldguestbook.html
Using the URLconf defined in mysite.urls, Django tried these URL patterns, in this order:
admin/
worldguestbook/
login/
addMessage/
The current path, addMessage/worldguestbook/worldguestbook.html, didn't match any of these.
注意:内容实际已提交,但未正确重定向。
【问题讨论】:
标签: django forms url redirect config