DJANGO中多种重定向方法使用

DJANGO中多种重定向方法使用,第1张

概述本文主要讲解使用HttpResponseRedirect、redirect、reverse以及配置文件中配置URL等重定向方法 使用场景,例如在表单一中提交数据后,需要返回到另一个指定的页面即可使用重

本文主要讲解使用httpResponseRedirect、redirect、reverse以及配置文件中配置URL等重定向方法

使用场景,例如在表单一中提交数据后,需要返回到另一个指定的页面即可使用重定向方法

一、 使用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)#这样的带参数重定向是可行的。
总结

以上是内存溢出为你收集整理的DJANGO中多种重定向方法使用全部内容,希望文章能够帮你解决DJANGO中多种重定向方法使用所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1213818.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-04
下一篇 2022-06-04

发表评论

登录后才能评论

评论列表(0条)

保存