Since 2.0, djano uses captured values in url paths.
urlpatterns = [
path('articles/2003/', views.special_case_2003),
path('articles/<int:year>/', views.year_archive),
]
You can, of course, still use regular expressions if the built-in path converters (str
, int
, slug
, uuid
, path
) aren't specific enough. In the case of url-safe base 64 encoding, slug may match, but if you need .
, =
and ~
then you can define your own:
convertors.py
class b64Converter:
# decide on complexity of your b64 regex by
# referring to https://stackoverflow.com/a/475217
regex = 'define_your_regex here'
def to_python(self, value):
return base64.urlsafe_b64decode(value)
def to_url(self, value):
return base64.urlsafe_b64encode(value)
then urls.py:
from django.urls import path, register_converter
from . import converters, views
register_converter(converters.b64Convertor, 'b64')
urlpatterns = [
path('widgets/<b64:my_url_param>/', views.widgetview),
...
]