I have multiple Images
associated with a Spots
using the intermediate table ImageSpots
, and I am trying to render each image for each spot but keep getting the error "The QuerySet value for an exact lookup must be limited to one result using slicing.". Can someone help me figure out what I'm doing wrong?
models.py
class Spots(models.Model):
title = models.CharField(max_length=155)
metatitle = models.CharField(max_length=155)
slug = models.SlugField(unique=True, max_length=155)
author = models.ForeignKey(Authors, models.DO_NOTHING)
field_created = models.DateTimeField(db_column='_created', blank=True, null=True)
field_updated = models.DateTimeField(db_column='_updated', blank=True, null=True)
cover = models.ImageField(upload_to="cover", blank=True, default='logo-00-06.png')
summary = models.TextField(blank=True, null=True)
content1 = models.TextField(blank=True, null=True)
content2 = models.TextField(blank=True, null=True)
class Meta:
managed = True
db_table = 'spots'
verbose_name_plural = 'Spots'
def __str__(self):
return str(self.id) + ": " + str(self.title)
class Images(models.Model):
note = models.CharField(max_length=100, blank=True, null=True)
image = models.ImageField(upload_to="images", blank=True, default='logo-00-06.png')
class Meta:
managed = True
db_table = 'images'
verbose_name_plural = 'Images'
def __str__(self):
return str(self.id) + ": " + str(self.note)
class ImageSpots(models.Model):
images = models.ForeignKey('Images', models.DO_NOTHING)
spots = models.ForeignKey('Spots', models.DO_NOTHING)
class Meta:
managed = True
db_table = 'image_spots'
verbose_name_plural = 'ImageSpots'
def __str__(self):
return str(self.spots) + ": " + str(self.images)
views.py
def article(request, slug):
article = get_object_or_404(Articles, slug=slug)
spots = Spots.objects.filter(articlespots__article=article).distinct()
images = Images.objects.filter(imagespots__spots=spots)
context = {'spots': spots, 'article': article, 'images':images}
return render(request, 'articletemplate.html', context)
html
<main class="page"></main>
<p>{{ article.title }}</p>
{% for spots in spots %} {{ spots.title }}
{% for images in images %}
<img style="width: 200px; height: 200px" src="{{ images.image.url }}" />
{% endfor %} {% endfor %}