App下 urls.py
# -*- coding: utf-8 -*-
from django.urls import path, re_path
from . import views
urlpatterns = [
re_path('(?P<year>[0-9]{4})/(?P<month>[0-9]{2})/(?P<day>[0-9]{2}).html',views.ShowIndexHTML)
]
参数 | 意义 |
---|---|
?P | 固定格式,P为大写 |
<xxx> | 变量名 |
[I-K]{x} | 取值 I-K,长度为 X |
views.py
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def ShowIndexHTML(request, year, month, day):
return HttpResponse(str(year) + '-' + str(month) + '-' + str(day))
此处路由地址的末端设置了 .html,这是一种伪静态路由技术,可将网址设置为静态网址,且为变量day设置终止符,否则浏览器输入无限字符路由仍能正常访问