Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
331 views
Welcome To Ask or Share your Answers For Others

1 Answer

等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...