Py学习  »  Django

[精华] django1.8 出现NoReverseMatch异常

mugbya • 8 年前 • 4169 次点击  
环境: python 3.4.3  django-1.8.2

在展示详细内容时出现这个异常:

NoReverseMatch at /
Reverse for 'detail' with arguments '()' and keyword arguments '{'pk_id': 2}' not found. 1 pattern(s) tried: ['$qs/(?P<pk_id>[0-9]+)/$']

其中有关的文件如下:

index.html

{% for item in post_latest %}
   <h1 class="title transition"><a href="{% url 'detail'  pk_id=item.pk %}">{{item.brief}}</a></h1>
{% endfor %}

urls.py

urlpatterns = [
    url(r'^$', views.IndexView.as_view(), name='index'),

    url(r'^qs/(?P<pk_id>[0-9]+)/$', views.detail, name='detail'),

]

views.py

def detail(request, pk_id):
    detail_qs = get_object_or_404(Qs, pk=pk_id)
    return render(request, 'qs/node/detail.html', {'detail': detail_qs})

当把index.html中的内容改成如下时,则没有这个异常

<h1 class="title transition"><a href="qs/{{item.id}}/">{{item.brief}}</a></h1>

但是按照django的规范中,还是最开始的写法符合,但是却一直报异常,google未果,所以求帮助

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/1112
 
4169 次点击  
文章 [ 4 ]  |  最新文章 8 年前