Skip to content
Advertisement

How do you get queryset objects related to queryset passed to templates in Django

I have these two models and as you can see they have a relationship.

JavaScript

As far as I understand if I query posts and push them to a template and post, I would expect to use something like this in my templates to retrieve the images URL attached to the posts but it doesn’t seem to work.

JavaScript

What am I doing wrong?

Here is my views.py file. views.py

JavaScript

Advertisement

Answer

The default related name for a foreign key relational is the name of the model (PostImage) but in your template for loop you called it post_image Following relationships “backward”

change

JavaScript

into

JavaScript

Template code (with change)

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement