繁体   English   中英

评论未显示在post_detail视图中

[英]Comments not showing in post_detail view

出于锻炼原因,我正在Django 1.9.9 / python 3.5中进行项目,我有一个博客应用程序,一个文章应用程序和一个评论应用程序。 评论应用必须与博客和文章相关。 我的问题是模板没有显示我的评论。 正在创建评论并将其与其文章/文章相关,因为我可以在admin中看到它,因此这不是评论创建问题。 它们根本没有显示在我的模板中。

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

假设模板中的post引用Entry模型的实例。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM