2013-02-26 18 views
7

Ho difficoltà a configurare il mio url per visualizzare una vista di dettaglio. Cliccando su questo link: <a href='{% url blog_detail blog.slug %}'>{{ blog.name }}</a> mostra blog.html, quando ho pensato che avrebbe mostrato blog-detail.html. Non ci sono errori e la barra del browser dice: example.com/blog/the-slug, ma mostra ancora l'html da blog.html, non blog-detail.html. Tutte le idee perché? Grazie per le tue ideeDjango Url, Slug per dettagli Pagina

url:

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

vista:

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: uscita richiesto da @omouse

Questa è l'uscita dalla cliccando sul link. È esattamente lo stesso di blog.html, ma dovrebbe essere 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

provare questo {{ blog.name }} catherine

+0

Sì già provato che, grazie. Ancora senza fortuna. Non sei sicuro di quale potrebbe essere il problema ... –

+0

puoi aggiungere l'output del template? –

risposta

22

gli URL sono il problema, il primo sarà abbinare tutto (/blog/, /blog/test/, /blog/awdlawdjaawld), è necessario il simbolo del dollaro $ alla fine di esso per solo partita /blog/.

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

Quanto sopra dovrebbe funzionare correttamente.

This is a good reference for Regular Expressions

+0

Ovviamente grazie! –