djangowagtaildjango-comments

'str' object has no attribute '_meta' error when setting up django-comments-xtd per documentation


My site is actually a wagtail site, although I'm not sure if that makes a difference. It's a simple blog application with a blog page to view posts and a page to show each post.

I was following the instructions here to setup django-comments-xtd

The documentation says to implement the following code to get a comment count displayed on each post page.

{% get_comment_count for object as comment_count %}
<div class="text-center" style="padding-top:20px">
  <a href="{% url 'blog:post-list' %}">Back to the post list</a>
  &nbsp;&sdot;&nbsp;
  {{ comment_count }} comments have been posted.
</div>

I changed the actual link to be the following, as that is what it was in my site (built from another tutorial)

<p><a href="{{ page.get_parent.url }}">Return to blog</a></p>

I don't think changing the url like that would cause the problem, from what I can tell. I have made sure to load comments at the start of the file also.

The actual error is:

Error during template rendering

In template /home/jake/blog/blog/post_page.html, error at line 8
'str' object has no attribute '_meta'

Line 8 refers to this line:

{% get_comment_count for object as comment_count %}

Could someone explain this error in more detail?


Solution

  • Django django-comments-xtd stores comments on objects. In your case the object is a Wagtail page. Change:

    {% get_comment_count for object as comment_count %}
    

    To:

    {% get_comment_count for page as comment_count %}
    

    The object variable is an empty string ''. This is an demo of what happens somewhere in the django-comments-xtd code:

    >>> ''._meta
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    AttributeError: 'str' object has no attribute '_meta'
    

    A Wagtail Page is both model and view. When a url is resolved Page.serve is called. That calls get_context and returns a dict.

    {'page': self, 'self': self, 'request': request}
    

    The context - all variables and their values - is used to fill the template. There is no object in the context!

    Django templates allow variables to be undefined. Empty vars won't throw an error. This concept can be useful. When the context doesn't supply a variable, it will default to an empty string.

    When documentation shows example code and mentions an {{ object }} or obj, they mean 'an object'. Any object (Pizzas, Cars, Questions). Your object. The default object in Wagtail is a Page object. You should use the page variable.