I have a for loop in my index.html file to dynamically fetch images from the images folder for three objects.
But it is only fetching the first image, with that missing image icon showing on the server for the other two 'destination' objects. Is there anything standing out as amiss in the index.html and views.py files?
All three images were showing when they were hardcoded and are all present in the images folder. It's since I introduced the {% static "images" as baseUrl %} and the for loop that it doesn't work.
It's just odd that it does indeed load the first image, but not image 2 and image 3.
I also tried:
src = "static/images/{{dest.img}}" instead of src = "{% static 'images/{{dest.img}}' %} and this didn't work.
Here is the for loop code with the top imports in index.html:
{% load static %}
{% static "images" as baseUrl %}
{% for dest in dests %}
<!-- Destination -->
<div class="destination item">
<div class="destination_image">
<img src="{{baseUrl}}/{{dest.img}}" alt="">
<div class="spec_offer text-center"><a href="#">Special Offer</a></div>
</div>
<div class="destination_content">
<div class="destination_title"><a href="destinations.html">{{dest.name}}</a></div>
<div class="destination_subtitle"><p>{{dest.desc}}</p></div>
<div class="destination_price">From ${{dest.price}}</div>
</div>
</div>
{% endfor %}
In my views.py file:
from django.shortcuts import render
from .models import Destination
def index(request):
dest1 = Destination()
dest1.name = 'New York'
dest1.desc = 'City that never sleeps'
dest1.img = 'destination_1.jpg'
dest1.price = 500
dest2 = Destination()
dest2.name = 'Mumbai'
dest2.desc = 'Great nightlife'
dest1.img = 'destination_2.jpg'
dest2.price = 800
dest3 = Destination()
dest3.name = 'Paris'
dest3.desc = 'The City Of Culinary Delights'
dest1.img = 'destination_3.jpg'
dest3.price = 700
dests = [dest1, dest2, dest3]
# 'dest1': dest1,
# 'dest2': dest2,
# 'dest3': dest3,
# }
return render(request, 'index.html', {'dests': dests})