• 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")),
    
    
    ]

  • 相关阅读:
    open_basedir restriction in effect的错误及其解决办法
    SNMP-网络管理协议
    安装cacti监控系统
    并发时-修改Linux系统下的最大文件描述符限制
    js new date()说明
    阿里云ECS环境部署 centos 6.5
    sysbench
    http_load
    LeetCode: Spiral Matrix
    LeetCode:Length of Last Word
  • 原文地址:https://www.cnblogs.com/mingerlcm/p/14169301.html
Copyright © 2020-2023  润新知