问题
I just begin to study Django, and today come to the comment part, I just practice from the Django Document.https://docs.djangoproject.com/en/1.4/ref/contrib/comments/example/. The command to add comment form works well when i do my practice on DetailView page but now I also want to add a comment form to ListView page then it got this error.
below is the traceback: Environment: Request Method: 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'
now I want to know how could i add every page a comment using the build-in comments??
回答1:
What you are saying then is that you pass appname.modelname to the template tag, you should actually pass an instance of the model to the template tag something like:
{% render_comment_list for publisher %}
if publisher is an instance of the Publisher model as it looks to be in your code. Or alternatively:
{% render_comment_list for books.publisher publisher.id %}
as psjinx mentioned above.
回答2:
render_comment_list
tag expects an object
but you are passing unicode string
. book.publisher
in Line 19, where exception is being raised` appears to be a string.
You can see valid arguments here. Here is the source code for this tag:
@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)
来源:https://stackoverflow.com/questions/14498212/attributeerror-str-object-has-no-attribute-meta