0

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 %}
haashe
  • 181
  • 2
  • 11

1 Answers1

0

{% for spots in spots %} should be {% for spot in spots %}

Edit:

I also spotted this error.

images = Images.objects.filter(imagespots__spots=spots)

should be:

images = Images.objects.filter(imagespots__spots__in=spots)

because ImageSpot has a ForeignKey to Spots which is a single object and you tried to filter it by giving it a list. So you need to use __in.

lucutzu33
  • 3,470
  • 1
  • 10
  • 24
  • It still gives me an error – haashe Oct 23 '21 at 09:04
  • Thank you that got rid of the error. But can you tell me how can I display images associated with the specific spot in HTML? At the moment the spots are different but images are all the same – haashe Oct 23 '21 at 12:04