2015-04-03 129 views
-1

這裏是我的urls.py:Django的NoReverseMatch

from django.conf.urls import patterns, include, url 

from articles import views 

urlpatterns = patterns('', 

    url(r'^$', views.PostList.as_view(), name='post'), 
    url(r'^(?P<post_id>\d+)/$', views.PostDetail.as_view(), name='postview'), 
) 

的index.html

{% for post in news_posts %} 
    <li id="article_title"><a href="{% url 'articles:postview' post.id %}">{{ post.post_title }}</a></li> 
    <li id="article_date">{{ post.post_date }}</li> 
    <hr> 
    <li id="article_image"><img src="static/media/{{ post.post_image }}" /></li> 
    <li id="article_shortdesc">{{ post.post_short_description }}</br><a href="">Read More>>></a></li> 
    <hr> 
{% endfor %} 

views.py

class PostList(generic.ListView): 
    model = newspost 
    template_name = 'articles/index.html' 
    context_object_name = "news_posts" 
    paginate_by = 5 

class PostDetail(generic.DetailView): 
    model = newspost 
    template_name = 'articles/articles.html' 

,我得到的是錯誤: 反向的「 postview'與參數'(1,)'和關鍵字參數'{}'找不到。 1種模式(S)嘗試:[U '(θP \ d +)/ $']

它還指出了我的錯誤是從該行未來:

<li id="article_title"><a href="{% url 'articles:postview' post.id %}">{{ post.post_title }}</a></li> 

我已經試着改變文章:postview到很多東西,並確保post.id是一個有效的變量。

回答

0

我進一步確診這一點,URL的Django試圖匹配這樣[u'$(?P\d+)/$']外觀:
$符號表示結束的字符串匹配的性格和它在你的URL的開頭。自帶記

的一件事是你,包括你的文章應用程式urls.py在你的主應用程序urls.py,如果這樣你就應該考慮這個問題:

  • 在文件的網址,你包括文章應用網址,你應該sometthing這樣的: url(r'^articles/$', include('articles.urls')) (在模式的結尾註意$標誌!)
    現在刪除$跡象,應該能順利通過!

希望這有助於!

+0

雖然post.id並未丟失,但爲了測試它,我只是將{{post.id}}放入我的模板中,併爲第一個帖子輸出1,第二個帖子輸出2。 – ZachPerkitny 2015-04-03 15:40:49

+0

對不起,看到編輯,我今天有完全相同的問題,我很久沒有!這就是我如何設法解決它,我希望這有助於。 – HassenPy 2015-04-03 17:42:51

+0

謝謝,那工作! – ZachPerkitny 2015-04-04 01:11:51