django-URL reverse lookup Reverse (IX)

Ease the path of the parameters.

reverse(viewname,urlconf=None,args=None,Kwargs=None,current_app=None)

book/views.py

from django.http import HttpResponse
from 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,'b':200}))

def error(request,a,b):
    sum=a+b
    return HttpResponse("<h1>sum:{}</h1>".format(sum))

book/urls.py

from django.urls import path
from . import views

urlpatterns = [
    path('', views.index,name='index'),
    path('error/<int:a>/<int:b>', views.error,name='loose'),
]

General process: After starting the server views the index function calls, since there is no username parameter, will be redirected to the loose (views.error namespace), i.e. error function calls, then there are two parameters a, b, need reverse to be able to pass on.

Guess you like

Origin www.cnblogs.com/xiximayou/p/11737948.html