zoukankan      html  css  js  c++  java
  • Django的路由层 路由控制之分发

     在app01应用里新建一个urls.py 文件 存放app01的url和视图函数对应关系

    导入include函数

    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),  # 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),
    
    ]

    app01 views.py

    from django.shortcuts import render, HttpResponse
    
    # Create your views here.
    
    
    def special_case_2003(request):
        
        return HttpResponse("special_case_2003")
    
    
    def year_archive(request, year):
    
        return HttpResponse(year)
    
    
    def month_archive(request, mouth, year):
    
        return HttpResponse(year+"-"+mouth)

    全局urls.py

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

    不加app01 方式访问

    全局urls.py

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

  • 相关阅读:
    待整理
    字符编码 【ZZ】
    python中的数据类型,存储,实现
    python中的浅拷贝和深拷贝
    算法比较-SVM和logistic回归
    机器学习中的范数规则化之(一)L0、L1与L2范数
    全排列的编码和解码----康托编码
    C++的const类成员函数
    Trie树的简单描述(需后续总结)
    UWP 手绘视频创作工具技术分享系列
  • 原文地址:https://www.cnblogs.com/mingerlcm/p/14169301.html
Copyright © 2011-2022 走看看