[Fixed]-Images from ImageField doesn't load in a template

1👍

When you write:

{{ offer.images.url }}

what you get is a blank string as offer.images is a RelatedManager and it does not have an url attribute.

You should retrieve the corresponding image either using a dedicated Manager or another query in your view:

main_images = offer.images.filter(main=True)
try:
    image = main_images[0]
except:
    image = None # Or whatever you want

You can also to avoid this to add an additional attribute main_image to your Offer model (but you will have to write the corresponding logic for admin).

Leave a comment