18👍
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'),
]
8👍
You should have a group name in the url pattern:
url(r'^(?P<date>\d{4}-\d{2}-\d{2})/$', views.index, name='index'),
# ^^^^
Also pay attention to the trailing slash in the url: www.mydomain.com/2011-02-12/
. If you don’t want a slash in the url, you can remove it from the pattern.
And your view function would then take the name of the group as one of its parameters
def index(request, date):
...
You should see the docs on Django url named groups
- Django querysets + memcached: best practices
- How to Serialize BigIntegerField, TextField in serializer Django
- Django – using multiple foreign key to the same model
- How to create multiple workers in Python-RQ?
1👍
try this :
url(r'^(?P<date>[0-9]{4}-?[0-9]{2}-?[0-9]{2})/$', views.index, name='index'),
- Tracking changes to all models in Django
- Sudo pip install django
- Django Forms: Foreign Key in Hidden Field
- Django finds tests but fail to import them
Source:stackexchange.com