Django中几种重定向方法


Posted in Python onApril 28, 2015

这里使用的是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 相关文章推荐
Python使用multiprocessing创建进程的方法
Jun 04 Python
python制作websocket服务器实例分享
Nov 20 Python
python实现Floyd算法
Jan 03 Python
python thrift搭建服务端和客户端测试程序
Jan 17 Python
Python装饰器原理与简单用法实例分析
Apr 29 Python
python 画三维图像 曲面图和散点图的示例
Dec 29 Python
Python2比较当前图片跟图库哪个图片相似的方法示例
Sep 28 Python
Python帮你识破双11的套路
Nov 11 Python
Spring Cloud Feign高级应用实例详解
Dec 10 Python
记一次django内存异常排查及解决方法
Aug 07 Python
python 使用三引号时容易犯的小错误
Oct 21 Python
Lombok插件安装(IDEA)及配置jar包使用详解
Nov 04 Python
详解Python的单元测试
Apr 28 #Python
Python xlrd读取excel日期类型的2种方法
Apr 28 #Python
Python发送email的3种方法
Apr 28 #Python
Python中使用partial改变方法默认参数实例
Apr 28 #Python
调试Python程序代码的几种方法总结
Apr 28 #Python
解析Python中的异常处理
Apr 28 #Python
python调用java模块SmartXLS和jpype修改excel文件的方法
Apr 28 #Python
You might like
php做下载文件的实现代码及文件名中乱码解决方法
2011/02/03 PHP
基于Zend的Config机制的应用分析
2013/05/02 PHP
ThinkPHP查询返回简单字段数组的方法
2014/08/25 PHP
PHP将字符分解为多个字符串的方法
2014/11/22 PHP
浅谈php中的循环while、do...while、for、foreach四种循环
2016/11/05 PHP
PHP实现将多个文件中的内容合并为新文件的方法示例
2017/06/10 PHP
PHP中strtr与str_replace函数运行性能简单测试示例
2019/06/22 PHP
在Laravel 的 Blade 模版中实现定义变量
2019/10/14 PHP
addEventListener()第三个参数useCapture (Boolean)详细解析
2013/11/07 Javascript
checkbox全选所涉及到的知识点介绍
2013/12/31 Javascript
javascript读写json示例
2014/04/11 Javascript
JavaScript 开发工具webstrom使用指南
2014/12/09 Javascript
快速掌握Node.js事件驱动模型
2016/03/21 Javascript
jQuery实现的手风琴侧边菜单效果
2017/03/29 jQuery
JavaScript生成图形验证码
2020/08/24 Javascript
js实现的在本地预览图片功能示例
2019/11/09 Javascript
js实现时钟定时器
2020/03/26 Javascript
小程序Scroll-view上拉滚动刷新数据
2020/06/21 Javascript
[25:59]Newbee vs TNC 2018国际邀请赛小组赛BO2 第二场 8.16
2018/08/17 DOTA
[01:06:30]DOTA2-DPC中国联赛定级赛 Phoenix vs DLG BO3第二场 1月9日
2021/03/11 DOTA
windows系统下Python环境的搭建(Aptana Studio)
2017/03/06 Python
python获取当前运行函数名称的方法实例代码
2017/04/06 Python
详解如何在python中读写和存储matlab的数据文件(*.mat)
2018/02/24 Python
python 实现在Excel末尾增加新行
2018/05/02 Python
对pandas的行列名更改与数据选择详解
2018/11/12 Python
Python如何调用JS文件中的函数
2019/08/16 Python
pytorch: Parameter 的数据结构实例
2019/12/31 Python
鱼油专家:Omegavia
2016/10/10 全球购物
美国生日蛋糕店:Bake Me A Wish!
2017/02/08 全球购物
马来西亚排名第一的宠物用品店:Pets Wonderland
2020/04/16 全球购物
人事主管岗位职责范本
2013/12/04 职场文书
毕业生的自我评价范文
2013/12/31 职场文书
奥巴马上海演讲稿
2014/09/10 职场文书
运动会通讯稿100字
2015/07/20 职场文书
教育教学工作反思
2016/02/24 职场文书
Nginx如何获取自定义请求header头和URL参数详解
2022/07/23 Servers