• django 路由控制之反向解析 python中方法


    全局urls.py 看app01 url

    from django.contrib import admin
    from django.urls import path, re_path, include
    
    from app01 import views
    
    urlpatterns = [
        path('admin/', admin.site.urls),
        # path("login.html/", views.login, name="logins"),
    
        # 分发
        re_path(r"^app01/", include("app01.urls")),
    
    
    ]

    给url 起个name

    app01的 urls.py

    from django.contrib import admin
    from django.urls import path, re_path
    
    from app01 import views
    
    urlpatterns = [
    
        re_path(r'^articles/2003/$', views.special_case_2003, name="s_c_2003"),  # special_case_2003(request)
        # re_path(r'^articles/([0-9]{4})/$', views.year_archive),  # year_archive(request, 2009)
        # re_path(r'^articles/([0-9]{4})/([0-9]{2})/$', views.month_archive),  # month_archive(request, 2009, 12)
    
        # month_archive(request, year=2009, month=12)
        # re_path(r'^articles/(?P<year>[0-9]{4})/(?P<mouth>[0-9]{2})/$', views.month_archive),
        # re_path(r'^articles/([0-9]{4})/([0-9]{2})/([0-9]+)/$', views.article_detail),
    
    
    ]

    views.py 视图函数 导入模块 函数

    from django.urls import reverse
    reverse("别名")  加上别名就行 就可以反向解析url
    打印url
    
    
    from django.urls import reverse
    
    
    def special_case_2003(request):
    
        url = reverse("s_c_2003")
        print(url)
    
        return HttpResponse("special_case_2003")


    访问一下

    可以看到反向解析出来的url

     

     2.换另外一个url的别名访问

    app01的 urls.py

    from django.contrib import admin
    from django.urls import path, re_path
    
    from app01 import views
    
    urlpatterns = [
    
        re_path(r'^articles/2003/$', views.special_case_2003, name="s_c_2003"),  # special_case_2003(request)
        re_path(r'^articles/([0-9]{4})/$', views.year_archive, name="y_a"),  # year_archive(request, 2009)
        # re_path(r'^articles/([0-9]{4})/([0-9]{2})/$', views.month_archive),  # month_archive(request, 2009, 12)
    
        # month_archive(request, year=2009, month=12)
        # re_path(r'^articles/(?P<year>[0-9]{4})/(?P<mouth>[0-9]{2})/$', views.month_archive),
        # re_path(r'^articles/([0-9]{4})/([0-9]{2})/([0-9]+)/$', views.article_detail),
    
    
    ]

    app01 views.py 视图函数

    from django.shortcuts import render, HttpResponse
    
    # Create your views here.
    
    from django.urls import reverse
    
    
    def special_case_2003(request):
    
        url = reverse("s_c_2003")
        url = reverse("y_a")  # app01/articles/([0-9]{4})/                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           
        print(url)
    
        return HttpResponse("special_case_2003")

    访问页面报错

     

    url 解析不了正则表达式,url加上了正则表达式 需要在reverse()函数另外加参数, 用参数数字替换正则表达式 

    加上同样格式数字 例如是4位数的 就写4位数字

    用参数替换正则表达式 从而拿到一个固定url

    from django.shortcuts import render, HttpResponse
    
    # Create your views here.
    
    from django.urls import reverse
    
    
    def special_case_2003(request):
    
        url = reverse("s_c_2003")
        url = reverse("y_a", args=(4009,))  # app01/articles/([0-9]{4})/
        print(url)
    
        return HttpResponse("special_case_2003")

    加上参数后可以访问

  • 相关阅读:
    Django学习笔记第六篇--实战练习二--简易实现登录注册功能demo
    追踪溯源--抓住隐藏在NAT后面的罪犯
    Linux内核态、用户态简介与IntelCPU特权级别--Ring0-3
    Windows2008 IIS配置FTP站点
    .NET RSA解密、签名、验签
    Quartz.NET 入门
    使用Topshelf创建Windows服务
    xcode6 新建项目真机调试无法全屏
    .NET 二维码生成(ThoughtWorks.QRCode)
    iOS手机应用开发原型模板及开发流程
  • 原文地址:https://www.cnblogs.com/mingerlcm/p/14199533.html
Copyright © 2020-2023  润新知