Django 反向解析路由

app2.urls.py

复制代码
from django.urls import path, re_path
from . import views


urlpatterns = [
    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 HttpResponse
from django.shortcuts import render
from django.urls import reverse
def 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

复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div style="color:red;font-size:24px;">url_reverse</div>

<div>
    在html中使用url标签进行反向解析
    <br>
    {%url 'app2_url_reverse'%}

</div>


</body>
</html>
相关推荐
BYSJMG16 小时前
2026计算机毕设推荐:基于大数据的车辆二氧化碳排放量可视化分析系统
大数据·vue.js·python·mysql·django·课程设计
WangYaolove13142 天前
基于opencv的疲劳检测系(源码+文档)
python·mysql·django·毕业设计·源码
luoluoal2 天前
基于python的人脸识别的酒店客房入侵检测系统(源码+文档)
python·mysql·django·毕业设计·源码
Blossom.1183 天前
从数字大脑到物理实体:具身智能时代的大模型微调与部署实战
人工智能·python·深度学习·fpga开发·自然语言处理·矩阵·django
墨染青竹梦悠然3 天前
基于Django+vue的零食商城
python·django
WangYaolove13144 天前
基于Python的旅游城市关键词分析
python·django·毕业设计·源码·计算机源码
历程里程碑4 天前
滑动窗口---- 无重复字符的最长子串
java·数据结构·c++·python·算法·leetcode·django
DingYuan1015 天前
Django模板继承详解
django
WangYaolove13145 天前
基于python的漏洞扫描系统(源码+文档)
python·mysql·django·毕业设计·源码
上半场结束,中场已休息,下半场ing6 天前
111111111
django