简体   繁体   English

评论未显示在post_detail视图中

[英]Comments not showing in post_detail view

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. 出于锻炼原因,我正在Django 1.9.9 / python 3.5中进行项目,我有一个博客应用程序,一个文章应用程序和一个评论应用程序。 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. 正在创建评论并将其与其文章/文章相关,因为我可以在admin中看到它,因此这不是评论创建问题。 They are simply not showing in my template. 它们根本没有显示在我的模板中。

My comments/models.py: 我的意见/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

My commments/views.py: 我的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})

And here is my post_detail.html, where comments should be. 这是我的post_detail.html,应在其中添加注释。 I will not post article_detail.html because they are exactly the same: 我不会发布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 %}

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. 让我知道是否有其他文件可以帮助您,例如博客/模型,视图,尽管我认为问题不存在。

You've explicitly set the related name of comments on your post to post_comments . 您已将帖子上评论的相关名称明确设置为post_comments So you would have to access them like: 因此,您必须像这样访问它们:

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

This is assuming post in your template refers to an instance of the Entry model. 假设模板中的post引用Entry模型的实例。

暂无
暂无

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

相关问题 未找到“post_detail”的反转。 “post_detail”不是有效的视图函数或模式名称 - Reverse for 'post_detail' not found. 'post_detail' is not a valid view function or pattern name post_detail()视图缺少必需的位置参数: - post_detail() view missing required positional arguments: NoReverseMatch at /post/new Reverse for &#39;post_detail&#39; 未找到。 “post_detail”不是有效的视图函数或模式名称 - NoReverseMatch at /post/new Reverse for 'post_detail' not found. 'post_detail' is not a valid view function or pattern name NoReverseMatch at / Reverse for &#39;post_detail&#39; 未找到 - NoReverseMatch at / Reverse for 'post_detail' not found post_detail不显示图像 - post_detail doesnt display image 单击发送按钮“post_detail”时出错不是有效的视图 function 或模式名称 - error while clicking the send button 'post_detail' is not a valid view function or pattern name 我无法创建post_detail和post_list - i can't create a post_detail and post_list 如何在 django 中没有 post_detail 页面的 django 评论表单中获取“post id”(与评论模型相关的外键字段) - how can i get the “post id”(foreign key field connected with comment model) to the django comments form without post_detail page in django 未找到带有参数 &#39;(&#39;&#39;,)&#39; 的 &#39;post_detail&#39; 反转。 尝试了 1 个模式:[&#39;post/(?P<slug> [-\\\\w]+)/$&#39;] - Reverse for 'post_detail' with arguments '('',)' not found. 1 pattern(s) tried: ['post/(?P<slug>[-\\w]+)/$'] 反向使用关键字 arguments '{'pk': '', 'article_id': ''}' not found - Reverse for 'post_detail' with keyword arguments '{'pk': '', 'article_id': ''}' not found
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM