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

5👍

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.

3👍

For reference purpose like mine, just use

{% get_comment_count for NameOfModel as comment_count %}

where NameOfModel is exactly the way you define the model in the views.py (I am using django).

Leave a comment