Comments not showing in post_detail view
<p>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.</p>

<p>My comments/models.py:</p>

<pre><code>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
</code></pre>

<p>My commments/views.py:</p>

<pre><code>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})
</code></pre>

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

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

<p>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.</p>

<p>You've explicitly set the related name of comments on your post to <code>post_comments</code>. So you would have to access them like:</p>

<pre><code>{% for comment in post.post_comments.all %}
</code></pre>

<p>This is assuming <code>post</code> in your template refers to an instance of the <code>Entry</code> model.</p>

python django python-3.x django-templates django-views