[英]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.