Django的路由系统正则匹配

Django的路由系统正则匹配

路由

from django.urls import path, include, re_path

import blog.views

urlpatterns = [
	re_path(r'^$',views.index) #根路径index页面
    re_path('file/([0-9]{4})/$', blog.views.year),
    re_path('file/([0-9]{4})/([0-9]{2})/$', blog.views.month),
    re_path('file/([0-9]{4})/([0-9]{2})/([0-9]+)/$', blog.views.detail),
]

视图

def year(request, year_list):
    return HttpResponse('返回%s年的文章列表' % year_list)


def month(request, year_list, month_list):
    return HttpResponse('返回%s年%s月的文章列表' % (year_list, month_list))


def detail(request, year_list, month_list, detail_id):
    return HttpResponse('返回ID为%s的文章' % detail_id)
上一篇:Django反向解析(下)


下一篇:Django 路由 命名分组