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

  • 相关阅读:
    ab (ApacheBench)命令
    Linux yum apt-get 方式
    Linux 作业调度器 crond
    FastDFS 注意事项
    FastDFS 搭建
    FastDFS 基础知识
    JS判断web网站访问端是PC电脑还是手机
    C# Json数据反序列化为Dictionary并根据关键字获取指定值1
    C#委托的异步调用1
    C# Json数据反序列化为Dictionary并根据关键字获取指定值
  • 原文地址:https://www.cnblogs.com/mingerlcm/p/14169301.html
Copyright © 2011-2022 走看看