我必须在“ report_detail.html
”中显示“评论”
我使用Django 2.2,我曾尝试向views.py
添加一些代码,但失败了。该报告需要在下面显示注释,但是,我尝试在views.py
和report_detail.html
中添加一些代码,但是它无法正常工作,该怎么办?谢谢
models.py
class Report(models.Model):
done = models.TextField('what done')
willDo = models.TextField('will do')
date = models.DateTimeField(auto_now_add=True)
author = models.ForeignKey(
get_user_model(),
on_delete=models.CASCADE,
)
def __str__(self):
return self.done
def get_absolute_url(self):
return reverse('report_detail', args=[str(self.id)])
class Comment(models.Model):
report = models.ForeignKey(
Report,
on_delete=models.CASCADE,
related_name='comments',
)
comment = models.CharField(max_length=140)
author = models.ForeignKey(
get_user_model(),
on_delete=models.CASCADE,
)
def __str__(self):
return self.comment
def get_absolute_url(self):
return reverse('report_list')
views.py
class ReportDetailView(LoginRequiredMixin, DetailView):
model = Report
template_name = 'report_detail.html'
login_url = 'login'
report_detail.html
<div class="article-entry">
<h2>{{ object.done }}</h2>
<p>by {{ object.author }} | {{ object.date }}</p>
<p>{{ object.willDo }}</p>
</div>
答案 0 :(得分:1)
我认为您要做的就是这样:
<div class="article-entry">
<h2>{{ object.done }}</h2>
<p>by {{ object.author }} | {{ object.date }}</p>
<p>{{ object.willDo }}</p>
{% for comment in object.comments.all %}
<p>{{ comment.comment }}</p>
{% endfor %}
</div>