Skip to content
Advertisement

How to get object_list of another user using ListView in Django?

I am new to Django and I am creating a simple blog web application. I would like to get the blog post of another user (not the user that is Authenticated) using the get_queryset Method. I tried the script below but, it shows an empty list on the template. I am able to use get_queryset to show all the blogpost, but my main concern is to show all the blogpost of a specific user (not the user that is authenticated)

View.py

JavaScript

Model.py

JavaScript

Advertisement

Answer

You can pass the id of the user that you want to filter the queryset by in the url pattern

JavaScript

In your view you can access the user_id from the path via self.kwargs['user_id'] and use this to filter your queryset

JavaScript
Advertisement