Answer the question
In order to leave comments, you need to log in
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)
Answer the question
In order to leave comments, you need to log in
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 %}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question