2

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??

ryu
  • 651
  • 9
  • 23

2 Answers2

3

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)
pankaj28843
  • 2,458
  • 17
  • 34
  • I am sorry, i am not really understand these code.you can not care these traceback i have paste, and the point is how could i add a comment form/list/count to a normal page, below is the urls.py urlpatterns = patterns('', url(r'^publishers/$',ListView.as_view(model=Publisher,),name='publishers'), url(r'^publishers/(?P\d+)$', DetailView.as_view(model=Publisher, template_name='books/post.html')),) – ryu Jan 24 '13 at 15:40
  • (render_comment_form for object) works well for the url /publishers/2. but when it come to /publishers/ which could display all the item line by line, now i also want to add a build-in comment, then got confused. so do you have any suggestion for this ? – ryu Jan 24 '13 at 15:44
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.

CJ4
  • 2,485
  • 3
  • 26
  • 29
  • Yes, i am also trying to assign a instance and i have done just like you said but still not work. even though `{% render_comment_form for books.publisher publisher.id%}` not report error any more, but on the page, nothing is displayed...you know if it is DetailView then the current object could be represented by "object" it is just used to assign which object to bind to. but when it come to ListView I dont know how to represent the current object. do you have other suggestion? – ryu Jan 24 '13 at 16:38
  • If the objects do not have comments no comments would show in the comment list but if that is not the case you could also try this: {% get_comment_list for publisher as comment_list %}{% for comment in comment_list %}{{ comment }}{% endfor %} – CJ4 Jan 24 '13 at 16:52
  • really appreciate your early reply. and btw on the last reply i have updated to create a form. the code you paste i have tried before but this will prompt the same error message "'str' object has no attribute '_meta'". so do you have any experience to add a comment to a non-post page.. – ryu Jan 24 '13 at 17:04
  • could you tell me some other method to implement the aim to add a comment to a ordinary page which is not using post method. i think it should be a very common issue, but because i am a beginner to django. this issue really annoying me a lot.. – ryu Jan 24 '13 at 17:10
  • The comment template tag is supposed to work for most object out of the box. – CJ4 Jan 24 '13 at 20:28
  • buddy, thanks anyway. I publish a more common way to ask. now do you mind to help also [link](http://stackoverflow.com/questions/14515384/how-to-use-django-build-in-comment-framework-on-any-template). – ryu Jan 25 '13 at 04:38