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>

相关推荐

  1. Django

    2024-03-19 20:36:04       39 阅读
  2. Django——

    2024-03-19 20:36:04       46 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-19 20:36:04       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-19 20:36:04       100 阅读
  3. 在Django里面运行非项目文件

    2024-03-19 20:36:04       82 阅读
  4. Python语言-面向对象

    2024-03-19 20:36:04       91 阅读

热门阅读

  1. React——组件化

    2024-03-19 20:36:04       48 阅读
  2. 2079: [蓝桥杯2023初赛] 冶炼金属

    2024-03-19 20:36:04       41 阅读
  3. 小型路由器,为什么四个端口的IP在一个网段?

    2024-03-19 20:36:04       44 阅读
  4. Transformer大模型学习导引:光速掌握大模型

    2024-03-19 20:36:04       54 阅读
  5. 文件上传漏洞或预习文件包含漏洞

    2024-03-19 20:36:04       44 阅读
  6. Visual Studio 常用快捷键与设置

    2024-03-19 20:36:04       46 阅读