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

  • 相关阅读:
    让keep-alive不缓存
    vue tab echart始终显示100px解决办法
    加载图片失败显示默认图片
    element 文件上传传参数
    vue-cli axios 拦截器设置
    制作字体图标制作
    20道Java面试必考题
    Integer源码解析
    阿里P9架构师讲解从单机至亿级流量大型网站系统架构的演进过程
    代理详解
  • 原文地址:https://www.cnblogs.com/mingerlcm/p/14169301.html
Copyright © 2011-2022 走看看