首页 > Web开发 > 详细

django-URL重定向(八)

时间:2019-10-25 14:51:48      阅读:96      评论:0      收藏:0      [点我收藏+]

HttpResponseRedirect()不常用

redirect(to,permanent=False,*args,**kwargs)

to:指重定向的位置,可以是视图,也可以是url地址,也可以是一个模块。permanent默认值是False,代表是否永久重定向。

(*args,**kwargs在我其它博客中有讲)

book/views.py

from django.http import HttpResponse
from django.shortcuts import render,redirect

# Create your views here.
def index(request):
    username = request.GET.get("username")
    if username is not None:
        return HttpResponse("welcome!")
    else:
        return redirect(‘loose‘)

def error(request):
    return HttpResponse("<h1>你走丢了</h1>")

book/urls.py

from django.urls import path
from . import views

urlpatterns = [
    path(‘‘, views.index,name=‘index‘),
    path(‘error/‘, views.error,name=‘loose‘),
]

启动服务器后会自动重定向到error界面:

技术分享图片

 我们在浏览器传入username参数,即http://127.0.0.1:8000/?username=gong

技术分享图片

django-URL重定向(八)

原文:https://www.cnblogs.com/xiximayou/p/11737856.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!