我刚刚在这里找到了Django博客教程:Blog tutorial
我现在正在尝试添加一项功能,该功能将计算博客帖子收到的观看次数。为此,我将以下内容添加到views.py中(请注意,本教程中的views.py完全为空,因为它使用Django通用视图):
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))
不确定我是否正确执行了render_to_response。我的urls.py现在是:
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")
我的post_list.html包含以下代码,用于创建指向post_detail.html的链接:
<a href={% url post object.id %}>{{ object.title }}</a>
完整的错误是:
> 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.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
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.
我一直试图找出几个小时的错误,但没有找到答案。我也在SO.com上阅读了很多东西,但仍然没有运气。
非常感谢您的帮助。
谢谢!
答案 0 :(得分:0)
尝试:
<a href="{% url post object_id=object.id %}">{{ object.title }}</a>
您需要提供参数的名称,以使反向快乐。另外,您应引用该网址。
P.S。如果您的URL条目是:
,您的原始代码将起作用url('^(\d+)/$', 'posts.views.detail_view', name="post")
但这不是推荐的方法。
此外,看起来你应该这样做:
... , {'object':object[0]}, ...
在render_to_response
来电中。 filter
只返回一个修改后的查询集,它可以与update
一起使用,但在您的模板中,您将其视为一个对象,因此在将其放入上下文时应将其解析为对象。