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>
相关推荐
Red丶哞9 小时前
[Django Message超全总结教程](武沛齐老师)
数据库·django·sqlite
曲幽10 小时前
Django入门指南:Python Web开发的“瑞士军刀”
python·django·flask·fastapi·web·pythonweb
一代土怪12 小时前
django中实时更新数据库
python·django
@zulnger14 小时前
Django 模型
后端·python·django
计算机毕业编程指导师14 小时前
【计算机毕设选题】基于Spark的拉勾网招聘数据分析系统源码,Python+Django全流程
大数据·hadoop·python·spark·django·招聘·拉勾网
xj75730653315 小时前
Django 面试常见问题
python·面试·django
QQ4022054961 天前
python基于vue的大学生课堂考勤系统设计与实现django flask pycharm
vue.js·python·django
WangYaolove13141 天前
基于自适应svm电影评价倾向性分析(源码+文档)
python·django·毕业设计·源码
开开心心就好1 天前
内存清理工具开源免费,自动优化清理项
linux·运维·服务器·python·django·pdf·1024程序员节
开开心心_Every1 天前
图片批量压缩工具:支持有损无损两种模式
python·游戏·微信·django·pdf·excel·语音识别