解决path中带参数的路径。
reverse(viewname,URLconf=None,args=None,Kwargs=None,current_app=None)
book/views.py
from django.http import HttpResponse django.shortcuts import render,redirect,reverse # Create your views here. def index(request): username = request.GET.get("username") if username is not None: return HttpResponse(welcome!else: return redirect(reverse('loose',kwargs={a':100,b200})) def error(request,a,b): sum=a+b <h1>sum:{}</h1>".format(sum))
book/urls.py
django.urls import path . import views urlpatterns = [ path('',views.index,name=index'),path(error/<int:a>/<int:b>'),
]
大体过程:启动服务器后会调用views中index函数,由于没有username参数,会重定向到loose(views.error的命名空间),即会调用error函数,此时有两个参数a,b,需要通过reverse才能够传下去。