Python + Django 页面重定向 [英] Python + Django page redirect

查看:23
本文介绍了Python + Django 页面重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How do I accomplish a simple redirect (e.g. cflocation in ColdFusion, or header(location:http://) for PHP) in Django?

推荐答案

很简单:

from django.http import HttpResponseRedirect

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

官方 Django 文档中的更多信息

更新:Django 1.0

现在使用 generic views 在 Django 中显然有更好的方法来做到这一点.

There is apparently a better way of doing this in Django now using generic views.

示例 -

from django.views.generic.simple import redirect_to

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

    #etc...
)

通用视图文档.信用 - Carles Barrobés.

更新 #2:Django 1.3+

在 Django 1.5 中 redirect_to 不再存在并被 重定向视图.归功于 Yonatan

In Django 1.5 redirect_to no longer exists and has been replaced by RedirectView. Credit to Yonatan

from django.views.generic import RedirectView

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

这篇关于Python + Django 页面重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆