Django HttpResponseRedirect

发布于 2024-11-04 03:13:28 字数 627 浏览 1 评论 0原文

我创建了一个基本的联系表单,当用户提交信息时,它应该重定向到“谢谢”页面。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

为你鎻心 2024-11-11 03:13:28

应该重定向的不是 POST 按钮,而是视图。

如果没有不同地指定,表单(HTML 表单标记)将 POST 到同一 URL。如果表单位于 /contact/ 上,则它会在 /contact/ 上 POST(带或不带斜杠,都是一样的)。

您应该重定向到感谢。从文档中:

def contact(request):
    if request.method == 'POST': # If the form has been submitted...
        form = ContactForm(request.POST) # A form bound to the POST data
        if form.is_valid(): # All validation rules pass
            # Process the data in form.cleaned_data
            # ...
            return HttpResponseRedirect('/thanks/') # Redirect after POST
    else:
        form = ContactForm() # An unbound form

    return render_to_response('contact.html', {
        'form': form,
    })

/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:

def contact(request):
    if request.method == 'POST': # If the form has been submitted...
        form = ContactForm(request.POST) # A form bound to the POST data
        if form.is_valid(): # All validation rules pass
            # Process the data in form.cleaned_data
            # ...
            return HttpResponseRedirect('/thanks/') # Redirect after POST
    else:
        form = ContactForm() # An unbound form

    return render_to_response('contact.html', {
        'form': form,
    })

Change /thanks/ to /contact/thanks/ and you're done.

月亮是我掰弯的 2024-11-11 03:13:28

所有响应都是正确的,但更好的方法是在 urls.py 中为 URL 命名,并在具有反向功能的视图中提示它们(而不是在视图中硬编码 URL)。

urls.py:

(r'^contact/

并在views.py中提示它们,如下所示:

from django.urls import reverse

return HttpResponseRedirect(reverse('app_name:thanks'))

这对于未来的方法更好,并遵循Django 的 DRY 原则

, contact, name='contact'), (r'^contact/thanks/

并在views.py中提示它们,如下所示:


这对于未来的方法更好,并遵循Django 的 DRY 原则

, contact_thanks, name='thanks'),

并在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:

(r'^contact/

And hint them in views.py like this:

from django.urls import reverse

return HttpResponseRedirect(reverse('app_name:thanks'))

This is better for future approach and follow the DRY principle of Django.

, contact, name='contact'), (r'^contact/thanks/

And hint them in views.py like this:


This is better for future approach and follow the DRY principle of Django.

, contact_thanks, name='thanks'),

And hint them in views.py like this:

This is better for future approach and follow the DRY principle of Django.

∞觅青森が 2024-11-11 03:13:28

我相信除了Aviral Dasgupta的解决方案之外,OP还需要更改相对url。

 return HttpResponseRedirect('/thanks/')

 return HttpResponseRedirect('/contact/thanks/')

/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.

 return HttpResponseRedirect('/thanks/')

to

 return HttpResponseRedirect('/contact/thanks/')

/thanks/ should take the url to root: yoursite/thanks/ and not yoursite/contact/thanks/.

小…红帽 2024-11-11 03:13:28

试试这个吧。这对我有用。

return HttpResponseRedirect('thanks/')

注意:- 删除前面的正斜杠

Just try this. It worked for me.

return HttpResponseRedirect('thanks/')

Note:- Remove the forward slash before

勿忘初心 2024-11-11 03:13:28

使用 Django APPEND_SLASH< /a> 设置。

APPEND_SLASH

当设置为True时,如果请求URL
与中的任何模式都不匹配
URLconf 并且它不以
斜杠,HTTP 重定向被发送到
附加斜杠的相同 URL。
请注意,重定向可能会导致任何
POST 请求中提交的数据
丢失了。

Use the Django APPEND_SLASH setting.

APPEND_SLASH

When set to True, if the request URL
does not match any of the patterns in
the URLconf and it doesn't end in a
slash, an HTTP redirect is issued to
the same URL with a slash appended.
Note that the redirect may cause any
data submitted in a POST request to be
lost.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文