I am using Django 3.2
I have created a custom Date URL pattern matcher as follows:
from datetime import datetime
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
from django.urls import path, register_converter
from . import views
from myproj.helpers import DateConverter
app_name = 'event'
register_converter(DateConverter, 'yyyymmdd')
urlpatterns = [
path('', views.index, name='index'),
path('detail/<int:event_id>/<slug:event_slug>', views.detail, name='detail'),
path('archive/<yyyymmdd:start_date>/<yyyymmdd:end_date>', views.archive, name='archive'),
]
<div class="row">
<a href="{% url 'event:archive' '2020101' '2020201' %}">Previous Events</a>
</div>
I get the following error:
reverse for 'archive' with arguments '('2020101', '2020201')' not found. 1 pattern(s) tried: ['media/events/archive/(?P<start_date>\d{4}\d{2}\d{2})/(?P<end_date>\d{4}\d{2}\d{2})$']
How do I fix this?
Is it possible you're not passing enough digits in? Your DateConverter requires 4 + 2 + 2 = 8 digits and the numbers you are passing in only have 7 digits.
Your date strings are wrong. Indeed, it is interpreted as:
2020101
YYYYMMDD
It is thus missing a digit for the month, a correct date thus looks like:
{% url 'event:archive' %}"
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.