Reputation: 1213
# models.py
from django.db import models
from . import constants
class Photos(models.Model):
band_member = models.ForeignKey(BandMember,on_delete=models.CASCADE)
photo = models.ImageField(upload_to='media/')
photo_description = models.TextField(max_length=400)
class Photos(models.Model):
band_member = models.ForeignKey(BandMember,on_delete=models.CASCADE)
photo = models.ImageField(upload_to='media/')
photo_description = models.TextField(max_length=400)
@property
def photo_url(self):
if self.photo and hasattr(self.photo, 'url'):
return self.photo.url
# views.py
def member_photos(request,member_id):
current_member = BandMember.objects.get(pk=member_id)
photos = current_member.photos_set.all()
context = {
'all_photos': photos,
'current_member': current_member,
}
return render(request,'band_members/member_photos.html', context)
html file :
{% if all_photos %}
{% for foto in all_photos %}
<img class"" src="{{ foto.photo_url }}"
alt = {{foto.photo_url }}">
<h5>{{ foto.photo_description }}</h5>
{% endfor %}
Although , image is not displayed , alt displays the correct path of the file
What am i doing wrong ? What i'm trying to achieve , is when user clicks on a 'SHOW PHOTOS' button at a page containing informations about a specific band member
(site/band_member/member_id here ), another page loads (site/band_member/member_id/photos) , displaying all the photos of the current member.
NEW TO DJANGO !!! Thank you ! sorry for my 'bad english' language :)
Upvotes: 0
Views: 584
Reputation: 541
The html inside the loop is not correct. Try fixing that like this.
<img src="{{ foto.photo_url }}" alt ="{{foto.photo_url }}">
<h5>{{ foto.photo_description }}</h5>
Upvotes: 1