django2 vs django3 how to write path instead of url

so I'm trying to type this in django3 with path

url(r'^tag/(?P<slug>[-\w]+)/$', TagIndexView.as_view(), name='tagged')

I tried tag/slug but I guess it didn't work.


Solution 1:

What you describe here is the regex of the <slug:…> path converter, you can thus implement this with:

from django.urls import path

urlpatterns = [
    path('tag/<slug:slug>/', TagIndexView.as_view(), name='tagged')
]

or you can keep using the regex with re_path(…) [Django-doc]:

from django.urls import re_path

urlpatterns = [
    re_path(r'^tag/(?P<slug>[-\w]+)/$', TagIndexView.as_view(), name='tagged')
]