I just did the Django blog tutorial found here: Blog tutorial
I am now trying to add a functionality that will count the number of views a blog post has received. To do that I added the following to views.py (note that views.py is completely empty in the tutorial because it uses Django generic views):
def detail_view(request, object_id):
object = Post.objects.filter(id=object_id)
object.update(counter = F('counter') + 1)
return render_to_response('posts/post_detail.html', {'object':object}, context_instance=RequestContext(request))
Not sure if I did that render_to_response correctly. My urls.py is now:
from models import Post
queryset = {'queryset': Post.objects.order_by('-pub_date')}
urlpatterns = patterns('',
url('^$', 'django.views.generic.list_detail.object_list', queryset, name="posts"),
url('^(?P<object_id>\d+)/$', 'posts.views.detail_view', name="post")
My post_list.html has the following code that creates a link to post_detail.html:
<a href={% url post object.id %}>{{ object.title }}</a>
And the full error is:
> Exception Value:
Caught NoReverseMatch while rendering: Reverse for 'post' with arguments '('',)' and keyword arguments '{}' not found.
Traceback:
> Environment:
Request Method: GET
Request URL: localhost:8000/1/
Django Version: 1.3
Python Version: 2.6.1
Installed Applications:
['django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.comments',
'django.contrib.admin',
'Blog.posts',
'Blog.categories']
Installed Middleware:
('django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.middleware.csrf.CsrfResponseMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware')
Template error:
In template /Users/kevin/PycharmProjects/Blog/templates/posts/_post.html, error at line 3
Caught NoReverseMatch while rendering: Reverse for 'post' with arguments '('',)' and keyword arguments '{}' not found.
1 : <div id=”object_{{ object.id }}”>
2 : <h2>
3 : <a href= {% url post object.id %} >{{ object.title }}</a>
4 : <small>{{ object.pub_date|timesince }} ago</small>
5 : </h2>
6 : {{ object.body }}
7 : </div>
Traceback:
File "/Library/Python/2.6/site-packages/django/core/handlers/base.py" in get_response
111. response = callback(request, *callback_args, **callback_kwargs)
File "/Users/kevin/PycharmProjects/Blog/posts/views.py" in detail_view
12. return render_to_response('posts/post_detail.html', {'object':object}, context_instance=RequestContext(request))
File "/Library/Python/2.6/site-packages/django/shortcuts/__init__.py" in render_to_response
20. return HttpResponse(loader.render_to_string(*args, **kwargs), **httpresponse_kwargs)
File "/Library/Python/2.6/site-packages/django/template/loader.py" in render_to_string
188. return t.render(context_instance)
File "/Library/Python/2.6/site-packages/django/template/base.py" in render
123. return self._render(context)
File "/Library/Python/2.6/site-packages/django/template/base.py" in _render
117. return self.nodelist.render(context)
File "/Library/Python/2.6/site-packages/django/template/base.py" in render
744. bits.append(self.render_node(node, context))
File "/Library/Python/2.6/site-packages/django/template/debug.py" in render_node
73. result = node.render(context)
File "/Library/Python/2.6/site-packages/django/template/loader_tags.py" in render
127. return compiled_parent._render(context)
File "/Library/Python/2.6/site-packages/django/template/base.py" in _render
117. return self.nodelist.render(context)
File "/Library/Python/2.6/site-packages/django/template/base.py" in render
744. bits.append(self.render_node(node, context))
File "/Library/Python/2.6/site-packages/django/template/debug.py" in render_node
73. result = node.render(context)
File "/Library/Python/2.6/site-packages/django/template/loader_tags.py" in render
64. result = block.nodelist.render(context)
File "/Library/Python/2.6/site-packages/django/template/base.py" in render
744. bits.append(self.re开发者_JS百科nder_node(node, context))
File "/Library/Python/2.6/site-packages/django/template/debug.py" in render_node
73. result = node.render(context)
File "/Library/Python/2.6/site-packages/django/template/loader_tags.py" in render
159. return self.render_template(self.template, context)
File "/Library/Python/2.6/site-packages/django/template/loader_tags.py" in render_template
141. output = template.render(context)
File "/Library/Python/2.6/site-packages/django/template/base.py" in render
123. return self._render(context)
File "/Library/Python/2.6/site-packages/django/template/base.py" in _render
117. return self.nodelist.render(context)
File "/Library/Python/2.6/site-packages/django/template/base.py" in render
744. bits.append(self.render_node(node, context))
File "/Library/Python/2.6/site-packages/django/template/debug.py" in render_node
73. result = node.render(context)
File "/Library/Python/2.6/site-packages/django/template/defaulttags.py" in render
450. raise e
Exception Type: TemplateSyntaxError at /1/
Exception Value: Caught NoReverseMatch while rendering: Reverse for 'post' with arguments '('',)' and keyword arguments '{}' not found.
I have been trying to figure out what's wrong for hours, but have not found an answer. I've read a bunch of stuff here on SO.com as well, but still no luck.
Your help is much appreciated.
Thanks!
Try:
<a href="{% url post object_id=object.id %}">{{ object.title }}</a>
You need to provide the name for the parameter to make the reverse happy. Also, you should quote the URL.
P.S. Your original code would have worked if your URL entry was:
url('^(\d+)/$', 'posts.views.detail_view', name="post")
but that's not the recommended way to do it.
Also, it looks like you should do:
... , {'object':object[0]}, ...
in your render_to_response
call. filter
just returns a modified queryset, which works fine with update
but in your template you treat it like an object, so you should resolve it to an object when putting it in the context.
精彩评论