This is part of index.html:
{% for listing in active_listings %}
<tr>
<td>{{ listing.title }}</td>
<td>{{ listing.description }}</td>
<td>{{ listing.bid }}$</td>
{% if listing.image %}
<td>{{ listing.image }}</td>
<td><img src="{% static 'auctions/'|add:listing.image %}" alt="photo"></td>
<!-- Here !!! -->
{% else %}
<td>No photo.</td>
{% endif %}
</tr>
{% endfor %}
This is the corresponding views.py:
def index(request):
return render(request, "auctions/index.html", {
"active_listings": Listing.objects.filter(active=True),
})
And that is my Listing Model in models.py:
class Listing(models.Model):
CATEGORIES = [
('BO', 'Books'),
('TO', 'Toys'),
('EL', 'Electronics'),
('FA', 'Fashion'),
('HO', 'Home'),
]
title = models.CharField(max_length=64)
description = models.TextField(max_length=256)
bid = models.PositiveIntegerField()
image = models.ImageField(null=True, blank=True)
category = models.CharField(max_length=2, choices=CATEGORIES, null=True, blank=True)
active = models.BooleanField(default=True)
Why can't I add the variable listing.image in the static tag???
It gives me: "GET /static/ HTTP/1.1" 404 1634
.
What I want to have is /static/auctions/foo.jpeg
, if listing.image = 'foo.jpeg'
. How can I do this?