-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是一個有效的變量。
雖然post.id並未丟失,但爲了測試它,我只是將{{post.id}}放入我的模板中,併爲第一個帖子輸出1,第二個帖子輸出2。 – ZachPerkitny 2015-04-03 15:40:49
對不起,看到編輯,我今天有完全相同的問題,我很久沒有!這就是我如何設法解決它,我希望這有助於。 – HassenPy 2015-04-03 17:42:51
謝謝,那工作! – ZachPerkitny 2015-04-04 01:11:51