[Answered ]-Django: get_context_data for comments related post

1👍

You can filter with:

class ResultsView(FormMixin, DetailView):
    model = Post
    template_name = 'posts.html'
    form_class = CommentForm

    def get_context_data(self, **kwargs):
        return super().get_context_data(
            **kwargs, comms=self.object.comments.filter(is_active=True)
        )

and then render with:

{% for comment in comms %}
    # …
{% endfor %}

Note: It is normally better to make use of the settings.AUTH_USER_MODEL [Django-doc] to refer to the user model, than to use the User model [Django-doc] directly. For more information you can see the referencing the User model section of the documentation.


Note: The related_name=… parameter [Django-doc]
is the name of the relation in reverse, so from the User model to the Comment
model in this case. Therefore it (often) makes not much sense to name it the
same as the forward relation. You thus might want to consider renaming the author relation to comments.

Leave a comment