I've just started learning django and have a problem loading images using loops.
- In models.py I created a class called
Pictures
- In views.py I created 3 instance of the class(Pictures) where
img
attribute is set to 3 different image names stored in the static folder.
Now, I got stuck in loading the images in the template using for loop. apart from img attribute, I also created other attributes. These are the name and image description both of type string and they load just fine when rendered in the template using for loop.
My question is what should I put in inside the img html tag to load these 3 different images using for loop?
Initially, I was using the syntax below to load images directly from the static folder.
<img src="{% static 'img/image-name.jpg' %}" alt="" width="250" height="250">
Now, I want to load the names from the img attribute and for each iteration, I want each of the three different images to load in the template each loop. Now I am not sure what to put inside the src="???".
what i have so far and its working:
{% for image in images %}
<li>
<img src="{{image.img}}" alt="" width="250" height="250">
<!-- I am not sure which line of code to include in src="" -->
</li>
{% endfor %}
also tried creating a variable like this:
{% static "img" as baseUrl %}
<img src="{{baseUrl}}/{{image.img}}"