app2.urls.py
from django.urls import path, re_pathfrom . import viewsurlpatterns = [path('index', views.index, name='index'),path('url_reverse', views.url_reverse, name='app2_url_reverse'), #使用reverse()方法反向解析 ,name对于视图的reverse("app2_url_reverse")]
app2.views.py
from django.http import HttpResponsefrom django.shortcuts import renderfrom django.urls import reversedef index(request):return HttpResponse("app2 的index")# 反向解析def url_reverse(request):# 使用reverse()方法反向解析print('解析结果:'+ reverse("app2_url_reverse"))#使用reverse()方法反向解析 ,reverse("app2_url_reverse") 对应urls的 name='app2_url_reverse'return render(request, "2/url_reverse.html")
templates1/2/url_reverse.html
Title url_reverse在html中使用url标签进行反向解析
{%url 'app2_url_reverse'%}