Reputation: 11
So I want to display all the posts from the user, in their profile while logged in, but can't seem to figure out how to do it. This is my template:
{% for post in posts %}
{% if user.username == post.author %}
<div class="container col-lg-8 ml-lg-5">
<div class="box">
<a href="#"><img class="rounded-circle account-image" src="{{
post.author.profile.image.url }}" alt="" ></a>
<a href="#"><h4 style="float: left;">{{post.author}}</h4></a>
<small>{{post.date_posted}}</small>
<hr width="82%" align="right">
<br>
<div class=""> <p>{{post.content}}</p></div>
</div>
<br>
</div>
{% endif %}
{% endfor %}
And this is my model: class Post(models.Model):
content = models.TextField()
date_posted = models.DateTimeField(default=timezone.now)
author = models.ForeignKey(User, on_delete=models.CASCADE)
It doesn't show me any errors, it just doesn't work. When I try removing the if statement, it works, but shows me every post by every user
Upvotes: 0
Views: 247
Reputation: 13731
You're comparing the current user's username to the post's user instance. Try this instead:
{% if user == post.author %}
Edit: If you're not doing anything with the posts that the user didn't make, you should really do that filtering in the view so the template is simpler.
Upvotes: 2