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>
相关推荐
博观而约取10 小时前
Django 数据迁移全解析:makemigrations & migrate 常见错误与解决方案
后端·python·django
哈里谢顿15 小时前
Django REST Framework 中序列化器的Meta详解
django
博观而约取1 天前
Django ORM 1. 创建模型(Model)
数据库·python·django
小王子10241 天前
Django+DRF 实战:从异常捕获到自定义错误信息
后端·django·web开发
ઈ一笑ഒ1 天前
Django
django
伊织code2 天前
Wagtail - Django 内容管理系统
后端·python·django·wagtail
互联网搬砖老肖2 天前
Python 中如何使用 Conda 管理版本和创建 Django 项目
python·django·conda
沛沛老爹12 天前
Python Django全功能框架开发秘籍
python·django·orm·web开发·模板引擎·项目部署·表单处理
Q_Q196328847512 天前
python基于微信小程序的广西文化传承系统
开发语言·spring boot·python·微信小程序·django·flask