加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

django-URL重定向(八)

发布时间:2020-12-15 17:07:15 所属栏目:大数据 来源:网络整理
导读:HttpResponseRedirect()不常用 redirect(to,permanent=False,*args,**kwargs) to:指重定向的位置,可以是视图,也可以是url地址,也可以是一个模块。permanent默认值是False,代表是否永久重定向。 (*args,**kwargs在我其它博客中有讲) book/views.py from

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读