评论没有显示在post_detail视图

问题描述:

我正在做一个项目在Django 1.9.9/python 3.5,由于练习的原因我有一个博客应用程序,一篇文章应用程序和一个评论应用程序。评论应用必须与博客和文章通用相关。我的问题是模板没有显示我的评论。评论正在创建并与他们的帖子/文章相关,因为我可以在管理员中看到它,所以它不是评论创建问题。他们根本不在我的模板中显示。评论没有显示在post_detail视图

我的意见/ 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 

我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}) 

这里是我的post_detail.html,其中的意见应该是。我不会发布article_detail.html因为他们是完全一样的:

{% 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 %} 

让我知道如果任何其他文件将有助于你帮我,像博客/模型,视图,虽然我不认为这个问题在那儿。

您已明确将帖子上的评论相关名称设置为post_comments。所以,你将不得不对其进行访问,如:

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

这是在你的模板假设postEntry模型的实例。

+0

是的!非常感谢。我在Django中很新,所以有时候我找不到像这样的明显错误。 –

+0

没问题。如果您在很多领域以类似的方式使用'Article'和'Entry'(例如,它们都附带了Comment),请查看从基础模型中继承它们,然后将'Comment'外键基本模型,而不是每个都有一个字段。请参阅https://docs.djangoproject.com/en/1.10/topics/db/models/#multi-table-inheritance –