Comments not showing in post_detail view
Question: I am doing a project in django 1.9.9/python 3.5, for exercise reasons I have a
blog app, an articles app and a comments app. Comments app has to be genericly
related to blog and articles. My problem is that the templates are not showing
my comments. Comments are being created and related to their post/article
because I can see it in admin, so it is not a comment creation problem. They
are simply not showing in my template.

My comments/models.py:

    
    
    from django.db import models
    
    
    class Comment(models.Model):
    
        post = models.ForeignKey('blog.Entry',related_name='post_comments', blank=True, null=True)
        article = models.ForeignKey('articles.Article', related_name='article_comments', blank=True, null=True)
        body = models.TextField()
        created_date = models.DateTimeField(auto_now_add=True)
    
    def __str__(self):
        return self.body
    

My commments/views.py:

    
    
    from django.utils import timezone
    from django.shortcuts import render, get_object_or_404
    from django.shortcuts import redirect
    
    from .forms import CommentForm
    from .models import Comment
    from blog.models import Entry
    from articles.models import Article
    from blog.views import post_detail
    from articles.views import article_detail
    
    
    
    def article_new_comment(request, pk):
        article = get_object_or_404(Article, pk=pk)
        if request.method == "POST":
            form = CommentForm(request.POST)
            if form.is_valid():
                comment = form.save(commit=False)
                comment.article = article
                comment.created_date=timezone.now()
                comment.save()
                return redirect(article_detail, pk=article.pk)
        else:
            form=CommentForm()
        return render(request, 'comments/add_new_comment.html', {'form': form})
    
    
    
    def blog_new_comment(request, pk):
        post = get_object_or_404(Entry, pk=pk)
        if request.method == "POST":
            form = CommentForm(request.POST)
            if form.is_valid():
                comment = form.save(commit=False)
                comment.post = post
                comment.created_date = timezone.now()
                comment.save()
                return redirect(post_detail, pk=post.pk)
        else:
            form=CommentForm()
        return render(request, 'comments/add_new_comment.html', {'form': form})
    

And here is my post_detail.html, where comments should be. I will not post
article_detail.html because they are exactly the same:

    
    
    {% extends 'blog/base.html' %}
    {% block content %}
        <div class="post">
            {% if post.modified_date %}
                <div class="date">
                    {{ post.modified_date }}
                </div>
             {% else %}
                <div class="date">
                    {{ post.published_date }}
                </div>
            {% endif %}
            <a class="btn btn-default" href="{% url 'post_edit' pk=post.pk %}"><span class="glyphicon glyphicon-pencil"> Edit Post </span></a>
            <h1>{{ post.title }}</h1>
            <p>{{ post.text|linebreaksbr }}</p>
            <hr>
            <a class="btn btn-default" href="{% url 'new_blog_comment' pk=post.pk %}"><span class="glyphicon glyphicon-pencil"> Add Comment </span></a>
            {% for comment in post.comments.all %}
                <div class="comment">
                    <div class="date">{{ comment.created_date }}</div>
                    <p>{{ comment.body|linebreaksbr }}</p>
                </div>
            {% empty %}
                <p>No comments here yet</p>
            {% endfor %}
        </div>
    {% endblock %}
    

Let me know if any other file would help you to help me, like blog/models,
views, although I don't think the problem is there.


Answer: You've explicitly set the related name of comments on your post to
`post_comments`. So you would have to access them like:

    
    
    {% for comment in post.post_comments.all %}
    

This is assuming `post` in your template refers to an instance of the `Entry`
model.

