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>
相关推荐
Python智慧行囊2 小时前
Django 项目创建全攻略
django·web·开发
w236173460120 小时前
Django框架漏洞深度剖析:从漏洞原理到企业级防御实战指南——为什么你的Django项目总被黑客盯上?
数据库·django·sqlite
大叔_爱编程1 天前
p024基于Django的网上购物系统的设计与实现
python·django·vue·毕业设计·源码·课程设计·网上购物系统
noravinsc1 天前
e.g. ‘django.db.models.BigAutoField‘.
数据库·django
Q_Q19632884751 天前
python的漫画网站管理系统
开发语言·spring boot·python·django·flask·node.js·php
noravinsc2 天前
redis是内存级缓存吗
后端·python·django
百锦再2 天前
大数据技术的主要方向及其应用详解
大数据·linux·网络·python·django·pygame
noravinsc2 天前
django中用 InforSuite RDS 替代memcache
后端·python·django
Q_Q19632884752 天前
python的家教课程管理系统
开发语言·spring boot·python·django·flask·node.js·php
noravinsc2 天前
人大金仓数据库 与django结合
数据库·python·django