9

どういうわけか私はこのバグを解決できないようです。Django1.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 %}

シェルを開くと、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.slugではなくp.idが機能するのはなぜですか?

これがトレースバックです

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 に答える