zoukankan      html  css  js  c++  java
  • 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")

    加上参数后可以访问

  • 相关阅读:
    设计模式的原则和法则
    GoF的23种设计模式分类和功能
    2020年智慧电力解决方案
    【转载】「黑科技」智能防疫消毒机器人 技术方案介绍-disinfection robot
    【转载】如何让电力巡检机器人项目落地
    30多张图来了解Keil5的使用
    [数学学习与代码]最小二乘法--多元线性方程求解
    MTK-LCM 屏幕使用fbconfig/PanelMaster来调试LCM驱动
    MTK 使用iptable 命令来完成网络路由(android WIFI/4G分享网络)
    MTK(android init.rc) 写一个开机启动的服务
  • 原文地址:https://www.cnblogs.com/mingerlcm/p/14199533.html
Copyright © 2011-2022 走看看