Django中多种重定向方法使用
来源:互联网 发布:钢雨篷荷载计算软件 编辑:程序博客网 时间:2024/06/07 12:51
本文主要讲解使用HttpResponseRedirect、redirect、reverse以及配置文件中配置URL等重定向方法
本文使用了Django1.8.2
使用场景,例如在表单一中提交数据后,需要返回到另一个指定的页面即可使用重定向方法一、 使用HttpResponseRedirect
● fuhao The first argument to the constructor is required – the path to redirect to. This can be a fully qualified URL or an absolute path with no domain。”参数可以是绝对路径跟相对路径”
from django.http import HttpResponseRedirect @login_required def update_time(request): #表单处理OR逻辑处理 return HttpResponseRedirect('/') #跳转到主界面 #如果需要传参数return HttpResponseRedirect('/commons/index/?message=error')
二 redirect和reverse
from django.core.urlresolvers import reverse from django.shortcuts import redirect #https://docs.djangoproject.com/en/1.8.2/topics/http/shortcuts/ @login_required def update_time(request): #进行要处理的逻辑 return redirect(reverse('test.views.invoice_return_index', args=[])) #跳转到index界面 redirect 类似HttpResponseRedirect的用法,也可以使用 字符串的url格式 /..index/?a=addreverse 可以直接用views函数来指定重定向的处理函数,args是url匹配的值。
三、 其他
#其他的也可以直接在url中配置from django.views.generic.simple import redirect_to 在url中添加 (r'^test/$', redirect_to, {'url': '/author/'}), #我们甚至可以使用session的方法传值request.session['error_message'] = 'test' redirect('%s?error_message=test' % reverse('page_index')) #这些方式类似于刷新,客户端重新指定url。
四
#重定向,如果需要携带参数,那么能不能直接调用views中 url对应的方法来实现呢,默认指定一个参数。#例如view中有个方法baseinfo_account, 然后另一个url(对应view方法为blance_account)要重定向到这个baseinfo_account。#url中的配置:urlpatterns = patterns('', url(r'^index/', 'account.views.index_account'), url(r'^blance/', 'account.views.blance_account'), ) # views.py@login_required def index_account(request, args=None): #按照正常的url匹配这么写有点不合适,看起来不规范 if args: print args return render(request, 'accountuserinfo.html', {"user": user}) @login_required def blance_account(request): return index_account(request, {"name": "orangleliu"}) #测试为:#1 直接访问 /index 是否正常 (测试ok)#2 访问 /blance 是否能正常的重定向到 /index页面,并且获取到参数(测试ok,页面为/index但是浏览器地址栏的url仍然是/blance)#这样的带参数重定向是可行的。
阅读全文
1 1
- Django中多种重定向方法使用
- [django]urls.py 中重定向
- Django 1.5重定向
- django重定向
- 转发和重定向 方法的使用
- 使用SpringMVC时无参的重定向方法
- Sudo命令中使用重定向
- 输入输出重定向的多种方式
- 多种方法取消google搜索结果重定向|无需跳转直达目标网站
- 多种方法取消Google搜索结果重定向|无需跳转直达目标网站
- 重定向方法集
- C++重定向方法
- tomcat重定向方法
- PHP重定向方法
- 重定向telnet方法
- python中对list去重的多种方法
- struts2中 重定向
- struts2_action中重定向
- 【FirstKotlinApp】使用Kotlin封装6.0的权限请求流程
- Error:Execution failed for task ':transformClassesWithJarMergingForDebug'. > com.android.build.api.t
- JS登录表单验证 正则验证
- scrapy 的crawl模板模拟登陆
- 基于.net2.0的汽车电子CCP协议的封装
- Django中多种重定向方法使用
- 【步兵 shader】基础篇
- 《高质量 C++/C 编程指南 》阅读笔记(四)
- 2017年网易内推笔试编程题1
- 实现一个Promise-polyfill
- NOIP 2009 提高组 复赛 trade 最优贸易
- 个人初学bootstrap并搭建后台界面代码
- 数据结构-散列
- 比较器