[Solved] how can I get all post of user’s in django [closed]


So the answer depends on your model setup.

For example, if your models.py looks like this:

class Post(models.Model)
    user = models.ForeignKey(User)
    content = models.CharField(max_length=500)
    ...

    def __str__(self):
        return self.user

Then if you wanted all of the posts of the user that is logged in, you would use this code, for example, in your request you would use filter to filter out the appropriate posts that match a certain parameter (in this case user):

def index_page(request):
    logged_in_user = request.user
    logged_in_user_posts = Post.objects.filter(user=user)

    return render(request, 'index.html', {'posts': logged_in_user_posts})

Then, for example, you could manipulate this queryset in your index.html file as such:

<div>
{% for post in posts %}

    Username: {{ post.user.username }}
    Post: {{ post.content }}

    <br>

{% endfor %}
</div>

0

solved how can I get all post of user’s in django [closed]