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")

    加上参数后可以访问

  • 相关阅读:
    更好的处理 Python 多工程 import 依赖
    Django 前后台的数据传递
    Django传递数据给JS
    nodejs 设置跨域访问
    Node.js + Express
    前端用户输入校验--基于JQ
    git统计当前分支提交次数
    gitlab相关
    CentOS7使用firewalld打开关闭防火墙与端口
    puppeteer安装/爬取数据
  • 原文地址:https://www.cnblogs.com/mingerlcm/p/14199533.html
Copyright © 2011-2022 走看看