for some reason I cannot seem to solve this bug. I am using Django 1.5
由于某种原因,我似乎无法解决这个错误。我正在使用Django 1.5
The actual error I get is
我得到的实际错误是
Reverse for 'detail' with arguments '()' and keyword arguments '{u'slug': u'third-post'}' not found.
In my projest dir I have this urls.py
在我的projest目录中,我有这个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')),
)
The urls.py in my app folder looks like this
我的app文件夹中的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'
),
)
My post detail view looks like this
我的帖子详情视图如下所示
class PostDetailView(DetailView):
template_name = 'blogapp/post/detail.html'
def get_object(self):
return get_object_or_404(Post, slug__iexact = self.kwargs['slug'])
I have this in my html
我的HTML中有这个
{% for p in latest_posts %}
<li><a href="{% url 'blog:detail' p.slug %}">{{p.title}}</a></li>
{% endfor %}
I can open my shell and I get a url back
我可以打开我的外壳,然后我会收回一个网址
>>> from django.core.urlresolvers import reverse
>>> reverse('blog:detail', kwargs={'slug':'test'})
'/post/test/'
The weird thing is in my html this line works
奇怪的是在我的HTML中这条线有效
<li><a href="{% url 'blog:detail' p.id %}">{{p.title}}</a></li>
why would p.id work and not p.slug?
为什么p.id工作而不是p.slug?
Here is the traceback
这是追溯
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.
1 个解决方案
#1
18
Because your url codes did not support this character "-", it must be
因为您的网址代码不支持此字符“ - ”,所以它必须是
url(r'^post/(?P<slug>[\w\-]+)/$',
PostDetailView.as_view(),
name = 'detail'
),
#1
18
Because your url codes did not support this character "-", it must be
因为您的网址代码不支持此字符“ - ”,所以它必须是
url(r'^post/(?P<slug>[\w\-]+)/$',
PostDetailView.as_view(),
name = 'detail'
),