9

出于某种原因,我似乎无法解决这个错误。我正在使用 Django 1.5

我得到的实际错误是

Reverse for 'detail' with arguments '()' and keyword arguments '{u'slug': u'third-post'}' not found.

在我的项目目录中,我有这个 urls.py

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

# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
    # Examples:
    # url(r'^$', 'blog.views.home', name='home'),
    # url(r'^blog/', include('blog.foo.urls')),

    # Uncomment the admin/doc line below to enable admin documentation:
    url(r'^admin/doc/', include('django.contrib.admindocs.urls')),

    # Uncomment the next line to enable the admin:
    url(r'^admin/', include(admin.site.urls)),

    #include the blogapp url
    url(r'', include('blogapp.urls', namespace='blog')),
)

我的应用程序文件夹中的 urls.py 看起来像这样

urlpatterns = patterns ('',

    url(r'^$', 
            ListView.as_view(
                    queryset = Post.objects.order_by('-pub_date')[:5],
                    context_object_name = 'latest_posts',
                    template_name = 'blogapp/default.html'),
            name = 'index'
        ),

    url(r'^post/(?P<slug>\w+)/$',
            PostDetailView.as_view(),
            name = 'detail'
        ),
)

我的帖子详细信息视图如下所示

class PostDetailView(DetailView):

    template_name = 'blogapp/post/detail.html'

    def get_object(self):
        return get_object_or_404(Post, slug__iexact = self.kwargs['slug'])

我的 html 中有这个

{% for p in latest_posts %}
      <li><a href="{% url 'blog:detail' p.slug %}">{{p.title}}</a></li>
{% endfor %}

我可以打开我的 shell 并获得一个 url

>>> from django.core.urlresolvers import reverse
>>> reverse('blog:detail', kwargs={'slug':'test'})
'/post/test/'

奇怪的是在我的 html 中这条线有效

<li><a href="{% url 'blog:detail' p.id %}">{{p.title}}</a></li>

为什么 p.id 可以工作而不是 p.slug?

这是回溯

Environment:


Request Method: GET
Request URL: http://127.0.0.1:8000/

Django Version: 1.5
Python Version: 2.7.2
Installed Applications:
('django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.sites',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'django.contrib.admin',
 'django.contrib.admindocs',
 'south',
 'blogapp')
Installed Middleware:
('django.middleware.common.CommonMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware')


Template error:
In template .../Development/Python/Django/dev/blog/blogapp/templates/blogapp/default.html, error at line 11
   Reverse for 'detail' with arguments '()' and keyword arguments '{u'slug': u'third-post'}' not found.
   1 : {% extends "blogapp/base.html" %}


   2 : 


   3 : {% block title %}Blog Posts{% endblock %}


   4 : 


   5 : {% block body %}


   6 : 


   7 :     {% if latest_posts %}


   8 :         <p>Latest 5 blog posts</p>


   9 :             <ul>


   10 :                 {% for p in latest_posts %}


   11 :                     <li><a href=" {% url 'blog:detail' slug=p.slug %} ">{{p.title}}</a></li>


   12 :                 {% endfor %}


   13 :             </ul>


   14 :     {% else %}


   15 :         <p>No blog posts published.</p>


   16 :     {% endif %}


   17 : 


   18 : {% endblock %}


   19 : 


   20 : 

Traceback:
File ".../.virtualenvs/env1/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  140.                     response = response.render()
File ".../.virtualenvs/env1/lib/python2.7/site-packages/django/template/response.py" in render
  105.             self.content = self.rendered_content
File ".../.virtualenvs/env1/lib/python2.7/site-packages/django/template/response.py" in rendered_content
  82.         content = template.render(context)
File ".../.virtualenvs/env1/lib/python2.7/site-packages/django/template/base.py" in render
  140.             return self._render(context)
File ".../.virtualenvs/env1/lib/python2.7/site-packages/django/template/base.py" in _render
  134.         return self.nodelist.render(context)
File ".../.virtualenvs/env1/lib/python2.7/site-packages/django/template/base.py" in render
  830.                 bit = self.render_node(node, context)
File ".../.virtualenvs/env1/lib/python2.7/site-packages/django/template/debug.py" in render_node
  74.             return node.render(context)
File ".../.virtualenvs/env1/lib/python2.7/site-packages/django/template/loader_tags.py" in render
  124.         return compiled_parent._render(context)
File ".../.virtualenvs/env1/lib/python2.7/site-packages/django/template/base.py" in _render
  134.         return self.nodelist.render(context)
File ".../.virtualenvs/env1/lib/python2.7/site-packages/django/template/base.py" in render
  830.                 bit = self.render_node(node, context)
File ".../.virtualenvs/env1/lib/python2.7/site-packages/django/template/debug.py" in render_node
  74.             return node.render(context)
File ".../.virtualenvs/env1/lib/python2.7/site-packages/django/template/loader_tags.py" in render
  63.             result = block.nodelist.render(context)
File ".../.virtualenvs/env1/lib/python2.7/site-packages/django/template/base.py" in render
  830.                 bit = self.render_node(node, context)
File ".../.virtualenvs/env1/lib/python2.7/site-packages/django/template/debug.py" in render_node
  74.             return node.render(context)
File ".../.virtualenvs/env1/lib/python2.7/site-packages/django/template/defaulttags.py" in render
  284.                 return nodelist.render(context)
File ".../.virtualenvs/env1/lib/python2.7/site-packages/django/template/base.py" in render
  830.                 bit = self.render_node(node, context)
File ".../.virtualenvs/env1/lib/python2.7/site-packages/django/template/debug.py" in render_node
  74.             return node.render(context)
File ".../.virtualenvs/env1/lib/python2.7/site-packages/django/template/defaulttags.py" in render
  188.                         nodelist.append(node.render(context))
File ".../.virtualenvs/env1/lib/python2.7/site-packages/django/template/defaulttags.py" in render
  424.                         raise e

Exception Type: NoReverseMatch at /
Exception Value: Reverse for 'detail' with arguments '()' and keyword arguments '{u'slug': u'third-post'}' not found.
4

1 回答 1

19

因为你的url代码不支持这个字符“-”,它必须是

    url(r'^post/(?P<slug>[\w\-]+)/$',
        PostDetailView.as_view(),
        name = 'detail'
    ),
于 2013-03-08T05:31:11.263 回答