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>
相关推荐
unicrom_深圳市由你创科技16 小时前
使用Django框架构建Python Web应用
前端·python·django
ku_code_ku16 小时前
Django由于数据库版本原因导致数据库迁移失败解决办法
后端·python·django
LuckyLay18 小时前
1.2.2 高级特性详解——AI教你学Django
python·django·sqlite
【本人】18 小时前
Django基础(三)———模板
后端·python·django
言之。20 小时前
Django中get()与filter()对比
数据库·django·sqlite
小王子102420 小时前
Django 实战:I18N 国际化与本地化配置、翻译与切换一步到位
django·国际化·i18n·web开发
Mr数据杨21 小时前
【Dv3Admin】菜单管理集成阿里巴巴自定义矢量图标库
python·django
Q_Q19632884751 天前
python的抗洪救灾管理系统
开发语言·spring boot·python·django·flask·node.js·php
言之。1 天前
Django `transaction.atomic()` 完整使用指南
数据库·python·django