for some reason I cannot seem to solve this bug. I am using 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
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
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
{% 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
<li><a href="{% url 'blog:detail' p.id %}">{{p.title}}</a></li>
why would p.id work and not 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.
Because your url codes did not support this character "-", it must be
url(r'^post/(?P<slug>[\w\-]+)/$',
PostDetailView.as_view(),
name = 'detail'
),
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With