无法弄清楚为什么错误NoReverseMatch, 尝试在Home.html
页面上显示链接查看:
def show_article(request, article_id):
article = get_object_or_404(Article, id=article_id)
return render(request, 'blog/article.html', {'article': article})
URL:
urlpatterns = [
url(r'^$', views.home, name="home"),
url(r'^about/$', views.about, name='about'),
url(r'^contact/$', views.contact, name='contact'),
url(r'^article/(?P<article_id>[0-9]+)/$', views.show_article, name='article'),
]
Home.html中:
{% extends 'blog/base.html' %}
{% block contaner %}
<div class="NameT">Главная</div>
<p>
{% for article in articles %}
<h4><a href="{% url 'article' article_id %}">{{ article.title }}</a></h4>
<div><pre>{{ article.get_short_text }}</pre></div>
{% endfor %}
</p>
{% endblock %}
article.html:
{% extends "blog/base.html" %}
{% block contaner %}
<h1>{{ article.title }}</h1>
<pre>{{ article.text }}</pre>
{% endblock %}
例外:
Exception Type: NoReverseMatch
Exception Value:
Reverse for 'article' with arguments '('',)' and keyword arguments '{}' not found. 1 pattern(s) tried: ['blog/article/(?P<article_id>[0-9]+)/$']
答案 0 :(得分:4)
<h4><a href="{% url 'article' article_id %}">{{ article.title }}</a></h4>
错了,应该是:
<h4><a href="{% url 'article' article.id %}">{{ article.title }}</a></h4>
您将article_id
作为参数传递,但该变量不存在。但article.id
是文章的ID,它是一个有效的值,很可能是您需要的。