2013-02-26 14 views
7

Mam problem z konfigurowaniem adresu URL, aby wyświetlić widok szczegółów. Kliknięcie tego linku: <a href='{% url blog_detail blog.slug %}'>{{ blog.name }}</a> pokazuje blog.html, kiedy myślałem, że pokaże blog-detail.html. Nie ma błędów, a pasek przeglądarki mówi: example.com/blog/the-slug, ale nadal wyświetla html z blog.html, a nie blog-detail.html. Jakieś pomysły, dlaczego? Dzięki za twoje pomysły.Django Url, Slug dla szczegółów Strona

url:

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

odsłon:

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: wyjście z wnioskiem @omouse

Jest to wyjście z kliknięciu na link. To jest dokładnie to samo co blog.html, ale powinno być blog-detail.html. Argggg!

<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> 
+0

próbować ten jeden {{ blog.name }} catherine

+0

Tak już próbowałem, które dzięki. Wciąż nie ma szczęścia. Nie wiesz, jaki może być problem ... –

+0

czy możesz dodać, jakie są dane wyjściowe szablonu? –

Odpowiedz

22

Te adresy są problemem, pierwszy będzie pasował do wszystkiego (/blog/, /blog/test/, /blog/awdlawdjaawld), trzeba znak dolara $ w końcu go do tylko mecz /blog/.

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

Powyższe powinno działać poprawnie.

This is a good reference for Regular Expressions

+0

Oczywiście dzięki! –

Powiązane problemy