[py]django实现url跳转
2017-08-06 14:51
1566 查看
HttpResponseRedirect实现uri跳转
需求: 访问/index 跳转到 /home1.写url
from learn import views urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^index/', views.index), url(r'^home/', views.home), ]
2.写views
from django.shortcuts import render,HttpResponseRedirect,HttpResponse # Create your views here. def index(request): return HttpResponseRedirect("/home") def home(request): return HttpResponse("home page")
3.测试访问
http://127.0.0.1:8000/index/ 跳转到了http://127.0.0.1:8000/home/
注: 跳转uri同时可以传参
return HttpResponseRedirect('/commons/invoice_return/index/?message=error') #跳转到index界面
另官网说
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,也可以是绝对路径。
即
return HttpResponseRedirect("https://www.baidu.com/") #访问http://127.0.0.1:8000/index/ 跳转到了 https://www.baidu.com/[/code]redirect+reverse重定向
需求: 访问/index 跳转到 /home
1.写urlsfrom learn import views urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^index/', views.index), url(r'^home/', views.home,name="home"), # reverse是根据name字段解析的. ]
2,写viewsfrom django.shortcuts import render,HttpResponseRedirect,HttpResponse from django.core.urlresolvers import reverse from django.shortcuts import redirect # Create your views here. def index(request): # return HttpResponseRedirect("/home") # return HttpResponseRedirect("https://www.baidu.com/") return redirect(reverse('home', args=[])) def home(request): return HttpResponse("home page")
可以对比下HttpResponseRedirect实现方式:def old_add2_redirect(request, a, b): return HttpResponseRedirect( reverse('add2', args=(a, b)) )
相关文章推荐
- 用django实现url跳转的几种方法总结
- javaEE--request.getRequestDispatcher--URL后带参数实现自定义跳转
- 实战实现apache服务内部URL地址跳转
- Django 更改url.py后报错
- ASP.NET LinkButton控件实现url 地址动态跳转
- 通过 URL 协议实现从 Safari 等浏览器中跳转打开你的 app
- iOS使用自定义URL实现控制器之间的跳转
- 页面跳转传参:参数过长(cookie,url , AngularJs controller 传参实现)
- [技术分享 - ISA 篇] 如何通过ISA2006发布网站时实现URL跳转
- 点击多个URL跳转多个不同界面使用一个Activity实现
- Apache通过更改配置文件实现url跳转和域名跳转
- Including a Django app's url.py is resulting in a 404 (The current URL, rango/, didn't match any of)
- 常见PHP实现URL地址跳转代码
- 通过 URL 协议实现从 Safari 等浏览器中跳转打开你的 app
- VC++ 获取跳转后的url函数实现及相关资料
- 采用JS实现url跳转导致url丢失的问题
- Apache 文件不存在跳转URL(实现指定尺寸的图片不存在,就自动生成图片)
- Django处理URL过程与网站分页功能实现
- Android中TextView自动识别url且实现点击跳转
- django下不通过view直接从url跳转到template