无法在模板中显示评论
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了无法在模板中显示评论相关的知识,希望对你有一定的参考价值。
我正在通过从互联网上获取参考来制作帖子和评论模型。我创建了Post and Comment模型,在Django管理面板中看起来还可以。我可以添加帖子以及对任何特定帖子的评论。但是当我尝试在模板中的帖子下方(帖子详细信息视图下)显示评论时遇到麻烦。请帮助
models.py
class Post(models.Model):
author = models.ForeignKey(User,on_delete=models.CASCADE)
title = models.CharField(max_length=100)
content = RichTextField()
tags = models.CharField(max_length=50,blank=True,null=True)
date_posted = models.DateTimeField(default=timezone.now)
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse('post-detail',kwargs={'pk':self.pk})
class Comment(models.Model):
post = models.ForeignKey(Post,on_delete=models.CASCADE)
author = models.ForeignKey(User,max_length=50,on_delete=models.CASCADE)
text = models.TextField()
create_date = models.DateTimeField(default=timezone.now)
def get_absolute_url(self):
return reverse('discuss')
views.py
class PostDetailView(DetailView):
model = Post
def add_comment_to_post(request,pk):
return get_object_or_404(Post,pk=pk)
if request.method == "POST":
form = CommentForm(request.POST)
if form.is_valid():
comment = form.save(commit=False)
comment.post= post
comment.save()
return redirect('post-detail',pk=post.pk)
else:
form = CommentForm()
return render(request, 'discuss/comment_form.html',{'form':form})
def comment_remove(request,pk):
comment = get_object_or_404(Comment,pk=pk)
post_pk = comment.post.pk
comment.delete()
return redirect('post-detail', pk=post_pk)
post_detail.html
{% extends 'index.html' %}
{% block content %}
<article class="media content-section">
<div class="medaia-body">
<img class="rounded-circle article-img" src="{{ object.author.profile.image.url }}" alt="image not found">
<div class="article-metedata">
<a class="mr-2" href="{% url 'user-posts' object.author.username %}">{{object.author}}</a>
<small class="text-muted">{{ object.date_posted|date:"F d, Y"}}</small>
</div>
<h2 class="article-title">{{ object.title }}</h2>
<img class="query-img" src="{{ object.image.url }}" alt="image not found">
<p class="article-content">{{ object.content|safe }}</p>
</div>
</article>
{% if object.author == user %}
<div class="post-update-delete">
<a href="{% url 'post-update' object.id %}"><button class="btn btn-outline-primary">Edit Post</button></a>
<a href="{% url 'post-delete' object.id %}"><button class="btn btn-outline-primary">Delete Post</button></a>
</div>
{% endif %}
<hr>
<a class="btn btn-primary btn-comment" href="{% url 'add_comment_to_post' pk=post.pk %}">Add Comment</a>
<!-- ############################### ABOVE CODE IS WORKING ############################# -->
<!-- ########################## GETTING PROBLEM IN BELLOW CODE ######################### -->
{% for comment in object.comments.all %}
{% if user.is_authenticated %}
{{ comment.create_date }}
{{ comment.text|safe|linebreaks }}
{{ comment.author }}
{% endif %}
{% empty %}
<p>No Comment</p>
{% endfor %}
{% endblock %}
在post_deatil.html中,我也尝试过{%in post.comments.all%}的评论,但它也不起作用
答案
由于未指定related_name=…
parameter [Django-doc],因此默认情况下related_name=…
为related_name
,因此您可以通过以下方式遍历注释:
comment_set
注:通常,最好直接使用
{% for comment in object.comment_set.all %} … {% endfor %}
来引用用户模型,而不是直接使用settings.AUTH_USER_MODEL
[Django-doc]。有关更多信息,请参见settings.AUTH_USER_MODEL
。
以上是关于无法在模板中显示评论的主要内容,如果未能解决你的问题,请参考以下文章