正则路由

This commit is contained in:
mxjdi 2025-04-26 22:26:20 +08:00
parent b9a3cad0a4
commit c7aad21e87
2 changed files with 7 additions and 0 deletions

View File

@ -38,6 +38,10 @@ urlpatterns = [
#http://localhost:8000/blog2/2025/4/26/80 #http://localhost:8000/blog2/2025/4/26/80
path('blog2/<int:year>/<int:month>/<int:day>/<int:id>', helloWorld.views.blog2), path('blog2/<int:year>/<int:month>/<int:day>/<int:id>', helloWorld.views.blog2),
# 正则路由
# http://localhost:8000/blog3/2025/04/26
re_path('blog3/(?P<year>[0-9]{4})/(?P<month>[0-9]{2})/(?P<day>[0-9]{2})', helloWorld.views.blog3),
# 媒体路由 # 媒体路由
re_path('media/(?P<path>.*)', serve, {'document_root': settings.MEDIA_ROOT}, name='media'), re_path('media/(?P<path>.*)', serve, {'document_root': settings.MEDIA_ROOT}, name='media'),
] ]

View File

@ -13,3 +13,6 @@ def blog(request, id):
def blog2(request, year, month, day, id): def blog2(request, year, month, day, id):
return HttpResponse(str(year) + '/' + str(month) + '/' + str(day) + '/' + ' id是' + str(id) + "的博客页面") return HttpResponse(str(year) + '/' + str(month) + '/' + str(day) + '/' + ' id是' + str(id) + "的博客页面")
def blog3(request, year, month, day):
return HttpResponse(str(year) + '/' + str(month) + '/' + str(day) + "的博客页面")