django:临时重定向和永久重定向之间有什么区别?

标签 django redirect django-views httpresponse http-redirect

好的,如果您转到此处的文档:
https://docs.djangoproject.com/en/dev/topics/http/shortcuts/

并向下滚动以查看上面说的redirect()的最后一个示例
“默认情况下,redirect()返回一个临时重定向。以上所有形式均接受一个永久参数;如果设置为True,则将返回一个永久重定向:”

现在,临时重定向和永久重定向之间有什么区别?我正在使用它,以便当用户登录并通过身份验证时,将其重定向到登录页面。我应该改用HttpResponseRedirect()吗?使用redirect()而不是HttpResponseRedirect()有什么好处吗?

最佳答案

有两种方法可以返回301永久重定向:

from django.shortcuts import redirect

def my_view(request):
    # some code here
    return redirect('/some/url/', permanent=True)

https://docs.djangoproject.com/en/1.5/topics/http/shortcuts/#redirect

或者:
from django.http import HttpResponsePermanentRedirect

    def my_view(request):
        # some code here
        return HttpResponsePermanentRedirect('/some/url')

https://docs.djangoproject.com/en/1.5/ref/request-response/#django.http.HttpResponsePermanentRedirect

关于django:临时重定向和永久重定向之间有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19073353/

相关文章:

django - 如何使用 django 基于日期的通用 View

django - 什么是 session ID 以及如何在 Django 中获取它?

python - 为什么打印的 Inline::Python 函数重定向失败?

javascript - 表单提交后重定向

rest - 如何将 Nginx 中的 POST/PUT/... 重新路由到内部服务?

django - 如何根据用户组更改 Django 模板?

javascript - 在 django 中处理单个 html 表单的多个输入值

django - apache2 python 3.4.3 ubuntu 错误 wsgi no module named

Django 鹡鸰网站 map

python - 在 bulk_create Django 中捕获特定的失败记录