Python + Django 页面重定向

标签 python django redirect location

如何在 Django 中完成简单的重定向(例如 ColdFusion 中的 cflocation 或 PHP 中的 header(location:http://))?

最佳答案

很简单:

from django.http import HttpResponseRedirect

def myview(request):
    ...
    return HttpResponseRedirect("/path/")

更多信息见 official Django docs

更新:Django 1.0

现在使用 通用 View ,显然有更好的方法在 Django 中执行此操作。

例子-

from django.views.generic.simple import redirect_to

urlpatterns = patterns('',   
    (r'^one/$', redirect_to, {'url': '/another/'}),

    #etc...
)

generic views documentation 中有更多内容. 信用 - Carles Barrobés .

更新 #2:Django 1.3+

在 Django 1.5 中 redirect_to 不再存在,已被 RedirectView 取代.归功于 Yonatan

from django.views.generic import RedirectView

urlpatterns = patterns('',
    (r'^one/$', RedirectView.as_view(url='/another/')),
)

关于Python + Django 页面重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/523356/

相关文章:

c# - Btrieve 没有 Pervasive?

python - 请求帖子文件的 MIME 类型

python - 在 Flask (Python) 中重定向到包含 'variable part' 的 URL

php - 将用户重定向到特定国家/地区的子域

python - CSS 选择器无法识别数字

python - pip 无法安装 pylibmc

python - django-cors-headers 根本不工作

python - Django - 您可以在 OnetoOneField 上使用 on_delete 属性吗?

python - Django 表单验证 : making "required" conditional?

linux - 如何以编程方式将程序的输出重定向到 SSL 套接字