Django HttpResponseRedirect
我创建了一个基本的联系表单,当用户提交信息时,它应该重定向到“谢谢”页面。
views.py:
def contact(request):
# if no errors...
return HttpResponseRedirect('/thanks/')
urls.py:
(r'^contact/$', contact),
(r'^contact/thanks/$', contact_thanks),
两个页面都在硬编码的 URL 上工作。但是,当我在 /contact/
上提交表单时,它会重定向到 /contact
(没有结尾斜杠),这是一个不存在的页面(404 或错误页面告诉我需要一个斜线)。
它没有正确重定向的原因是什么?我该如何解决这个问题?
更新: return HttpResponseRedirect('/contact/thanks/')
是我现在所拥有的,但问题是提交按钮(使用 POST)不会重定向到URL——它根本不重定向。
I have created a basic contact form, and when the user submits information, it should redirect to the "Thank You" page.
views.py:
def contact(request):
# if no errors...
return HttpResponseRedirect('/thanks/')
urls.py:
(r'^contact/
Both pages work at the hard-coded URL. However, when I submit the form on /contact/
it redirects to /contact
(no ending slash), which is a nonexistent page (either a 404 or an error page telling me I need a slash).
What is the reason it not correctly redirecting, and how can I fix this?
UPDATE: the return HttpResponseRedirect('/contact/thanks/')
is what I now have, but the problem is that the submit button (using POST) does not redirect to the URL -- it doesn't redirect at all.
, contact),
(r'^contact/thanks/
Both pages work at the hard-coded URL. However, when I submit the form on /contact/
it redirects to /contact
(no ending slash), which is a nonexistent page (either a 404 or an error page telling me I need a slash).
What is the reason it not correctly redirecting, and how can I fix this?
UPDATE: the return HttpResponseRedirect('/contact/thanks/')
is what I now have, but the problem is that the submit button (using POST) does not redirect to the URL -- it doesn't redirect at all.
, contact_thanks),
Both pages work at the hard-coded URL. However, when I submit the form on /contact/
it redirects to /contact
(no ending slash), which is a nonexistent page (either a 404 or an error page telling me I need a slash).
What is the reason it not correctly redirecting, and how can I fix this?
UPDATE: the return HttpResponseRedirect('/contact/thanks/')
is what I now have, but the problem is that the submit button (using POST) does not redirect to the URL -- it doesn't redirect at all.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
应该重定向的不是 POST 按钮,而是视图。
如果没有不同地指定,表单(HTML 表单标记)将 POST 到同一 URL。如果表单位于 /contact/ 上,则它会在 /contact/ 上 POST(带或不带斜杠,都是一样的)。
您应该重定向到感谢。从文档中:
将
/thanks/
更改为/contact/thanks/
就完成了。It's not the POST button that should redirect, but the view.
If not differently specified, the form (the HTML form tag) POSTs to the same URL. If the form is on /contact/, it POSTs on /contact/ (with or without slash, it's the same).
It's in the view that you should redirect to thanks. From the doc:
Change
/thanks/
to/contact/thanks/
and you're done.所有响应都是正确的,但更好的方法是在 urls.py 中为 URL 命名,并在具有反向功能的视图中提示它们(而不是在视图中硬编码 URL)。
urls.py:
并在views.py中提示它们,如下所示:
这对于未来的方法更好,并遵循Django 的 DRY 原则。
All of the responses are correct but a better approach is to give names to your URLs in
urls.py
and hint to them in views with reverse function (instead of hard coding URL in views).urls.py:
And hint them in views.py like this:
This is better for future approach and follow the DRY principle of Django.
我相信除了Aviral Dasgupta的解决方案之外,OP还需要更改相对url。
到
/thanks/
应该将 URL 带到 root:yoursite/thanks/
而不是yoursite/contact/thanks/
。I believe that apart from Aviral Dasgupta's solution, OP also needs to change the relative url.
to
/thanks/
should take the url to root:yoursite/thanks/
and notyoursite/contact/thanks/
.试试这个吧。这对我有用。
注意:- 删除前面的正斜杠
Just try this. It worked for me.
Note:- Remove the forward slash before
使用 Django APPEND_SLASH< /a> 设置。
APPEND_SLASH
Use the Django APPEND_SLASH setting.
APPEND_SLASH