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>
相关推荐
troy1285 小时前
分布式系统框架选型指南:主流框架优缺点深度对比
python·django·pygame
IT毕设实战小研13 小时前
基于大数据的跨国外派人员适应满意度与留存影响因素可视化分析
android·java·大数据·python·django·课程设计
烂蜻蜓1 天前
Django入门教程(三):django-admin与manage.py命令完全指南
数据库·django·sqlite
2601_962283883 天前
Django数据库配置(一)
mysql·postgresql·django·sqlite·数据库配置
qq_22589174664 天前
基于Python+Django的LangGraph智能旅游规划系统
python·django·旅游
小刘在重生~5 天前
Django|Excel 批量上传、Form/ModelForm 文件上传、Media 媒体文件配置
python·django·excel
小刘在重生~5 天前
Django|Ajax 入门、JSON 返回、ECharts 图表、文件上传完整笔记
ajax·django·json
2601_962283885 天前
Python 开发框架:Django、Flask和FastAPI
python·django·flask·fastapi·web开发
小刘在重生~6 天前
Django 全套入门笔记|安装、项目创建、ORM、Form、中间件、登录认证
笔记·中间件·django
大模型丫丫7 天前
工业级 RAG 为什么需要 Elasticsearch?
elasticsearch·django·jenkins