2013-02-26 59 views
7

我無法配置我的網址以顯示詳細視圖。點擊此鏈接:<a href='{% url blog_detail blog.slug %}'>{{ blog.name }}</a>顯示blog.html,當時我認爲它會顯示blog-detail.html。沒有錯誤,瀏覽器欄顯示:example.com/blog/the-slug,但仍顯示來自blog.html的html,而不是blog-detail.html。任何想法爲什麼?感謝您的想法。Django Url,Slug for Detail Page

網址:

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

觀點:

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}) 

編輯:輸出由@omouse

要求這是從點擊鏈接輸出。它與blog.html完全一樣,但應該是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

試試這個{{ blog.name }} catherine 2013-02-26 02:04:47

+0

是已經嘗試過,謝謝。仍然沒有運氣。不知道可能是什麼問題... – 2013-02-26 02:06:51

+0

你可以添加模板輸出是什麼? – 2013-02-26 02:19:31

回答

22

的網址是問題,第一個將匹配一切(/blog//blog/test//blog/awdlawdjaawld),你需要在它的結束比賽/blog/美元符號$

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

以上應該正常工作。

This is a good reference for Regular Expressions

+0

當然,謝謝! – 2013-02-26 02:46:16