【问题标题】:Comments not showing in post_detail viewpost_detail 视图中未显示评论
【发布时间】:2017-01-26 08:57:00
【问题描述】:

我正在 django 1.9.9/python 3.5 中做一个项目,出于锻炼原因,我有一个博客应用程序、一个文章应用程序和一个 cmets 应用程序。评论应用程序必须与博客和文章大体相关。我的问题是模板没有显示我的 cmets。正在创建评论并与他们的帖子/文章相关,因为我可以在管理员中看到它,所以这不是评论创建问题。它们根本没有显示在我的模板中。

我的 cmets/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

我的评论/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,cmets 应该在的位置。我不会发布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 %}

让我知道是否有任何其他文件可以帮助您帮助我,例如博客/模型、视图,尽管我认为问题不存在。

【问题讨论】:

    标签: python django python-3.x django-templates django-views


    【解决方案1】:

    您已将帖子中 cmets 的相关名称明确设置为 post_comments。所以你必须像这样访问它们:

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

    这是假设您的模板中的 post 指的是 Entry 模型的实例。

    【讨论】:

    • 是的!太感谢了。我是 django 的新手,所以有时我找不到像这样明显的错误。
    • 没问题。如果您在许多领域以类似的方式使用ArticleEntry(例如,它们都附加了Comments),请查看从基本模型中对它们进行子类化,然后将Comment 外键化为基本模型,而不是每个都有一个字段。见docs.djangoproject.com/en/1.10/topics/db/models/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-03-10
    • 1970-01-01
    • 2018-03-11
    • 1970-01-01
    • 1970-01-01
    • 2015-05-24
    • 2013-06-05
    相关资源
    最近更新 更多