如何将评论显示在表单上

时间:2019-07-19 08:49:20

标签: django

我必须在“ report_detail.html”中显示“评论”

我使用Django 2.2,我曾尝试向views.py添加一些代码,但失败了。该报告需要在下面显示注释,但是,我尝试在views.pyreport_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>

1 个答案:

答案 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>