Django Url, Slug for Detail Page
Asked Answered
S

2

13

I'm having trouble configuring my url to display a detail view. Clicking on this link: <a href='{% url blog_detail blog.slug %}'>{{ blog.name }}</a> shows blog.html, when I thought it would show blog-detail.html. There are no errors and the browser bar says: example.com/blog/the-slug, yet still displays the html from blog.html, not blog-detail.html. Any ideas why? Thanks for your ideas.

url:

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

views:

def blog(request):
    blog_list = Blog.objects.all()
    return render(request, 'blog.html', {'blog_list':blog_list})

def blog_detail(request, slug):
    blog = get_object_or_404(Blog, slug=slug)
    return render(request, 'blog-detail.html', {'blog':blog})

EDIT: output requested by @omouse

This is the output from clicking on the link. It is exactly the same as blog.html, but it should be blog-detail.html.

<div id='content-wrapper'>
<section>
<div class='blog-name'><h2><a href='/blog/test/'>Test</a></h2></div>
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry&#39;s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a ...

<div class='blog-name'><h2><a href='/blog/second-test/'>Second Test</a></h2></div>
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry&#39;s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a ...
</section>
</div>
Sauropod answered 26/2, 2013 at 2:0 Comment(4)
try this one <a href='{% url blog_detail slug=blog.slug %}'>{{ blog.name }}</a>Guitarist
Yes already tried that thanks. Still no luck. Not sure what could be the issue...Sauropod
can you add what the template output is?Triciatrick
Thanks @omouse: the template output is blog.html. It is simply dummy text, but I have included it above.Sauropod
T
28

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

Triciatrick answered 26/2, 2013 at 2:41 Comment(0)
T
0

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.

Tremann answered 8/11, 2019 at 23:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.