Reputation: 35
I have to let the "comment" show in the report_detail.html
I an using Django 2.2 , I have tried to add some code to views.py
but failed. The Report need to show the comment below, however, I try add some code in views.py
and report_detail.html
but it can not work, how can I do ? thank you
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>
Upvotes: 0
Views: 50
Reputation: 962
I think that what you want to do is just this:
<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>
Upvotes: 1