未在第六部分中显示的评论

2024-04-27 03:30:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在django1.9.9/python3.5中做一个项目,出于锻炼的原因,我有一个blog应用程序、一个articles应用程序和一个comments应用程序。评论应用程序必须一般相关的博客和文章。我的问题是模板没有显示我的评论。正在创建评论并与他们的文章/文章相关,因为我可以在admin中看到它,所以这不是一个评论创建问题。他们只是没有显示在我的模板。你知道吗

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

我的命令/视图.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})

这是我的帖子_详细信息.html,此处应显示注释。我不会发表文章_详细信息.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 %}

让我知道如果有任何其他文件可以帮助你帮助我,如博客/模型,意见,虽然我不认为有问题。你知道吗


Tags: fromimportdivformtruedatemodelsrequest
1条回答
网友
1楼 · 发布于 2024-04-27 03:30:49

您已经显式地将帖子上的相关评论名设置为post_comments。所以你必须像这样访问它们:

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

这是假设模板中的post引用了Entry模型的实例。你知道吗

相关问题 更多 >