[Answer]-Django foreign key object image in template

1๐Ÿ‘

โœ…

# In models.py

class Venue(models.Model):
    ....
    name = models.CharField(max_length=100)
    ....
    images = models.ForeignKey(VenueImage)

class VenueImage(models.Model):
    image = models.ImageField(upload_to=VENUE_IMAGE_PATH)

# In views.py

def list(request):
    venues = Venue.objects.all()
    return render(request,
                  'venues/list.html',
                  {'venues': venues'})

# In template

{% for venue in venues %}
<a href '#'>{{ venue.name }}</a>
...
{% for image in venue.images %}
<img class='venue_image' src=''{{ image.url }}' alt=''>
{% endfor %}
{% endfor %}
๐Ÿ‘คRag Sagar

Leave a comment