R
R
Ruslan Bergutov2015-05-13 15:44:49
Django
Ruslan Bergutov, 2015-05-13 15:44:49

How can I display images by album on a single Django page?

There is such a model:

class Album(models.Model):
    title = models.CharField(max_length=255)
    caption = models.CharField(max_length=1000)
    slug = models.SlugField(primary_key=True, unique=True)

class Image(models.Model):
    image = models.ImageField(upload_to='albums/')
    title = models.CharField(max_length=255)
    caption = models.CharField(max_length=1000)
    album = models.ForeignKey(Album)

How can I display all images from all albums in different divs across albums on one page?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman Kitaev, 2015-05-13
@deliro

In the context you throw {'albums': Album.objects.all()}
In the template:

{% for album in albums %}
    <div class="album-{{ album.pk }}">
        {% for image in album.image_set.all %}
            <div class="image-{{ image.pk }}>
                <img src="{{ image.image.url }}}" alt="Image was here"/>
            </div>
        {% endfor %}
    </div>
{% endfor %}

Customize div classes to your liking.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question