In Django 3.0.5 I did this using the following urls.py
from django.urls import path, register_converter
from datetime import datetime
from . import views
class DateConverter:
regex = 'd{4}-d{2}-d{2}'
def to_python(self, value):
return datetime.strptime(value, '%Y-%m-%d')
def to_url(self, value):
return value
register_converter(DateConverter, 'yyyy')
urlpatterns = [
path('', views.index, name='index'),
path('date/<yyyy:date>/', views.date, name='date'),
]
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…