Django中几种重定向方法

yipeiwu_com6年前Python基础

这里使用的是django1.5

需求: 有一个界面A,其中有一个form B, 前台提交B之后,后台保存数据之后,返回界面A,如果保存失败需要在A界面提示错误。

这里就需要后台的重定向,而且需要可以带着参数,也就是error message
这里收集了几种方法,简答说下需要那些包,怎么简单使用。

一、 使用HttpResponseRedirect

The first argument to the constructor is required – the path to redirect to. This can be a fully qualified URL (e.g.'http://www.yahoo.com/search/') or an absolute path with no domain (e.g. '/search/')。 参数既可以使用完整的url,也可以是绝对路径。

复制代码 代码如下:

from django.http import HttpResponseRedirect 
 
@login_required 
def update_time(request): 
    #pass  ...   form处理 
    return HttpResponseRedirect('/commons/invoice_return/index/')  #跳转到index界面 

如果需要传参数,可以通过url参数
复制代码 代码如下:

return HttpResponseRedirect('/commons/invoice_return/index/?message=error')  #跳转到index界面 

这样在index处理函数中就可以get到错误信息。

二、 redirect和reverse

复制代码 代码如下:

from django.core.urlresolvers import reverse 
from django.shortcuts import redirect 
#https://docs.djangoproject.com/en/1.5/topics/http/shortcuts/ 
 
@login_required 
def update_time(request): 
    #pass  ...   form处理 
    return redirect(reverse('commons.views.invoice_return_index', args=[]))  #跳转到index界面 

redirect 类似HttpResponseRedirect的用法,也可以使用 字符串的url格式 /..inidex/?a=add
reverse 可以直接用views函数来指定重定向的处理函数,args是url匹配的值。 详细请参见文档

三、 其他

其他的也可以直接在url中配置,但是不知道怎么传参数。

复制代码 代码如下:

from django.views.generic.simple import redirect_to

在url中添加 (r'^one/$', redirect_to, {'url': '/another/'}), 

我们甚至可以使用session的方法传值

复制代码 代码如下:

request.session['error_message'] = 'test' 
redirect('%s?error_message=test' % reverse('page_index')) 

这些方式类似于location刷新,客户端重新指定url。
还没找到怎么在服务端跳转处理函数,直接返回response到客户端的方法。

2014-11-13 研究:

是不是之前的想法太死板,重定向,如果需要携带参数,那么能不能直接调用views中 url对应的方法来实现呢,默认指定一个参数。
例如view中有个方法baseinfo_account, 然后另一个url(对应view方法为blance_account)要重定向到这个baseinfo_account。

url中的配置:

复制代码 代码如下:

urlpatterns = patterns('', 
    url(r'^baseinfo/', 'account.views.baseinfo_account'), 
    url(r'^blance/', 'account.views.blance_account'), 


复制代码 代码如下:

@login_required 
def baseinfo_account(request, args=None): 
    ​#按照正常的url匹配这么写有点不合适,看起来不规范 
    ​if args: 
        print args 
    return render(request, 'accountuserinfo.html', {"user": user}) 
 
 
@login_required     
def blance_account(request): 
    return baseinfo_account(request, {"name": "orangleliu"}) 

需要测试为:
1 直接访问 /baseinfo 是否正常 (测试ok)
2 访问 /blance 是否能正常的重定向到 /baseinfo 页面,并且获取到参数(测试ok,页面为/baseinfo 但是浏览器地址栏的url仍然是/blance)

这样的带参数重定向是可行的。

相关文章

python 对类的成员函数开启线程的方法

如下所示: # -*- coding: utf-8 -*- import threading import thread import time class Test(objec...

Python导出数据到Excel可读取的CSV文件的方法

本文实例讲述了Python导出数据到Excel可读取的CSV文件的方法。分享给大家供大家参考。具体实现方法如下: import csv with open('eggs.csv', '...

关于Python中空格字符串处理的技巧总结

关于Python中空格字符串处理的技巧总结

前言 大家应该都知道字符串处理,是任何语言最常用到的。 其中就经常会碰到,对字符串中的空格处理,比如:去除前后空格,去除全部空格,或者以空格为分隔符来处理。 好在Python中字符串有很...

CentOS 6.5中安装Python 3.6.2的方法步骤

前言 centos 是自带python的。但是版本稍微旧一些。搞python开发,肯定要用新一点的稳定版。所以,要升级一下python。本文将介绍在CentOS 6.5中安装Python...

利用scrapy将爬到的数据保存到mysql(防止重复)

利用scrapy将爬到的数据保存到mysql(防止重复)

前言 本文主要给大家介绍了关于scrapy爬到的数据保存到mysql(防止重复)的相关内容,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍吧。 1.环境建立  ...