AttributeError:'str'对象没有属性'_meta'

时间:2013-01-24 09:45:03

标签: django django-comments

我刚开始研究Django,今天来到评论部分,我只是从Django文档练习.https://docs.djangoproject.com/en/1.4/ref/contrib/comments/example/。当我在DetailView页面上练习时,添加注释表单的命令很有效 但是现在我还想在ListView页面添加一个评论表单,然后就出现了这个错误。

下面是追溯:     环境:     请求方法:GET

Django Version: 1.4.3
Python Version: 2.7.3
Installed Applications:
('django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.admin',
'books',
'django.contrib.comments')
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 /home/ryu/emacs/emacs-code/djcode/mysite2/templates/books/publisher_list.html, error at line 19
'str' object has no attribute '_meta'


9 :   <th> website</th>


10 : </tr>


11 : {% for publisher in object_list %}


12 : <tr>


13 :   <th><a href="/mysite2/publishers/{{publisher.id}}">{{ publisher.name }}</a></th>  


14 :   <th>{{publisher.country}}</th>


15 :   <th>{{publisher.website}}</th>


16 : </tr> 


17 : {% endfor %}


18 : </table>


19 :  {% render_comment_list for books.publisher %} 


20 : {% endblock %}


21 : 
Traceback:
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in get_response
136.                     response = response.render()
File "/usr/local/lib/python2.7/dist-packages/django/template/response.py" in render
104.             self._set_content(self.rendered_content)
File "/usr/local/lib/python2.7/dist-packages/django/template/response.py" in       rendered_content
81.         content = template.render(context)
File "/usr/local/lib/python2.7/dist-packages/django/template/base.py" in render
140.             return self._render(context)
File "/usr/local/lib/python2.7/dist-packages/django/template/base.py" in _render
134.         return self.nodelist.render(context)
File "/usr/local/lib/python2.7/dist-packages/django/template/base.py" in render
823.                 bit = self.render_node(node, context)
File "/usr/local/lib/python2.7/dist-packages/django/template/debug.py" in render_node
74.             return node.render(context)
File "/usr/local/lib/python2.7/dist-packages/django/template/loader_tags.py" in render
123.         return compiled_parent._render(context)
File "/usr/local/lib/python2.7/dist-packages/django/template/base.py" in _render
134.         return self.nodelist.render(context)
File "/usr/local/lib/python2.7/dist-packages/django/template/base.py" in render
823.                 bit = self.render_node(node, context)
File "/usr/local/lib/python2.7/dist-packages/django/template/debug.py" in render_node
74.             return node.render(context)
File "/usr/local/lib/python2.7/dist-packages/django/template/loader_tags.py" in render
123.         return compiled_parent._render(context)
File "/usr/local/lib/python2.7/dist-packages/django/template/base.py" in _render
134.         return self.nodelist.render(context)
File "/usr/local/lib/python2.7/dist-packages/django/template/base.py" in render
823.                 bit = self.render_node(node, context)
File "/usr/local/lib/python2.7/dist-packages/django/template/debug.py" in render_node
74.             return node.render(context)
File "/usr/local/lib/python2.7/dist-packages/django/template/loader_tags.py" in render
62.             result = block.nodelist.render(context)
File "/usr/local/lib/python2.7/dist-packages/django/template/base.py" in render
823.                 bit = self.render_node(node, context)
File "/usr/local/lib/python2.7/dist-packages/django/template/debug.py" in render_node
74.             return node.render(context)
File "/usr/local/lib/python2.7/dist-          packages/django/contrib/comments/templatetags/comments.py" in render
201.         ctype, object_pk = self.get_target_ctype_pk(context)
File "/usr/local/lib/python2.7/dist- packages/django/contrib/comments/templatetags/comments.py" in get_target_ctype_pk
100.             return ContentType.objects.get_for_model(obj), obj.pk
File "/usr/local/lib/python2.7/dist-packages/django/contrib/contenttypes/models.py" in get_for_model
32.         opts = self._get_opts(model)
File "/usr/local/lib/python2.7/dist-packages/django/contrib/contenttypes/models.py" in   _get_opts
20.         return model._meta.concrete_model._meta

Exception Type: AttributeError at /mysite2/publishers/
Exception Value: 'str' object has no attribute '_meta'

现在我想知道如何使用内置注释添加注释?

2 个答案:

答案 0 :(得分:3)

render_comment_list代码需要object,但您要传递unicode string。第19行中的book.publisher,其中异常被提出`似乎是一个字符串。

您可以看到有效的参数here。以下是此标记的源代码:

@register.tag
def render_comment_list(parser, token):
    """
    Render the comment list (as returned by ``{% get_comment_list %}``)
    through the ``comments/list.html`` template

    Syntax::

        {% render_comment_list for [object] %}
        {% render_comment_list for [app].[model] [object_id] %}

    Example usage::

        {% render_comment_list for event %}

    """
    return RenderCommentListNode.handle_token(parser, token)

答案 1 :(得分:1)

您所说的是您将appname.modelname传递给模板标记,您实际上应该将模型的实例传递给模板标记,例如:

{% render_comment_list for publisher %}

如果发布者是Publisher模型的实例,因为它看起来在您的代码中。或者:

{% render_comment_list for books.publisher publisher.id %}

如上面提到的psjinx。