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


]

 

 

posted @ 2021-01-06 17:26  minger_lcm  阅读(89)  评论(0编辑  收藏  举报