[Fixed]-Django Url, Slug for Detail Page


The urls are the problem, the first one will match everything (/blog/, /blog/test/, /blog/awdlawdjaawld), you need the dollar sign $ at the end of it to only match /blog/.

url(r'^blog/$', 'myapp.views.blog', name='blog'),
url(r'^blog/(?P<slug>[\w-]+)/$', 'myapp.views.blog_detail', name='blog_detail'),

The above should work correctly.

This is a good reference for Regular Expressions


Rudolf absolutely right

The /$ stopped blog from catching all the subpages which called by slug
so if you have subpages you need to add /$ to folder level as follow:

re_path('brands/$', AllBrands.as_view(), name="brands"),
re_path(r'^brands/(?P<slug>[\w-]+)/$', BrandDetail.as_view(), name = 'brandetail'),

This is django 2.2

Without /$ after brands, the slug page was showing the brands listing page.

Leave a comment