Django Url, Slug for Detail Page

后端 未结 2 820
故里飘歌
故里飘歌 2020-12-14 11:08

I\'m having trouble configuring my url to display a detail view. Clicking on this link: {{ blog.name }}

相关标签:
2条回答
  • 2020-12-14 11:22

    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

    0 讨论(0)
  • 2020-12-14 11:26

    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.

    0 讨论(0)
提交回复
热议问题