Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

Social Media with Django

So I'm building a social media web application with django. Users are able to create an online library of the books they own, they can upload pictures and write book reviews. For books, pictures and reviews I have models al have a foreignkey to a user. The problem I am stumbling on right now is that when I show a class based Listview of the books, pictures and reviews I get all the data from the users as expected. But my question is. how do I show a listview of objects that a specific user has uploaded? For example, if someone goes to see someone's instagram gallery, they only show the pictures uploaded by that spesific user.

I hope my question is clear enough? Because I can't seem to figure out the logic behind this.

Jeff Muday
Jeff Muday
Treehouse Moderator 28,720 Points

Cool idea, I hope when you get done you will show it off to fellow students!

There are two good sections in the Django documentation that pertain to what you want to do -- Subsets and Dynamic Filtering. Specifically "Dynamic Filtering" example looks to be what you are really trying to do. They use an example of "books/<publisher_name>" as the URL. Then you specifically override the method "get_queryset(self)". Inside that method, you are going to get the Username coded in the URL from self.args[0]. Now... the second method to override is "get_context_data(self, **kwargs)" which will allow you to pass on class variables to the template via the context dictionary-- you will need the extra context data for "decorating" your template with the username (title, headings, etc).

Subsets of Objects https://docs.djangoproject.com/en/1.11/topics/class-based-views/generic-display/#viewing-subsets-of-objects

Dynamic Filtering https://docs.djangoproject.com/en/1.11/topics/class-based-views/generic-display/#dynamic-filtering

1 Answer

Jeff Muday Hi,, thanks for you respons. Quite a simple solutions actually, lol ... It seems to work fine only using the get_queryset method in the view. If I add the dynamic url path it gives me an error, but I will check tomorrow morning why that is.. Thanks for helping ;) .. And I surely will share my final product with the community here