So i have 2 models,
class Post(models.Model):
topic = models.CharField(max_length=200)
description = models.TextField()
created_by = models.ForeignKey(User, related_name='posts')
created_on = models.DateTimeField()
def __str__(self):
return self.topic
class Comment(models.Model):
commented_by = models.ForeignKey(User, related_name='comments')
commented_on = models.ForeignKey(Post, related_name='comments')
commented_text = models.CharField(max_length=500)
commented_time = models.DateTimeField(auto_now_add=True)
def __str__(self):
return self.commented_text
and i want to display post and comment on single template. So, i created this view:
def post_desc(request, pk):
post = get_object_or_404(Post, pk=pk)
comment_list = Comment.objects.all()
return render(request, 'post_desc.html', {'post': post, 'comment_list':comment_list})
and try to display them in template:
{% if post.created_on %}
{{ post.created_on }}
{% endif %}
<h1>{{ post.topic }}</h1>
<p>{{ post.description }}</p>
<h4>Comments</h4>
{% if comment_list.commented_on == post.topic %}
{{ comment_list.commented_by }}{{ comment_list.commented_text }}
{% endif %}
But it is only displaying post and not comment section. What i did wrong here?