# 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文件:
{% if all_photos %}
{% for foto in all_photos %}
<img class"" src="{{ foto.photo_url }}"
alt = {{foto.photo_url }}">
<h5>{{ foto.photo_description }}</h5>
{% endfor %}
尽管未显示图像,但alt显示了文件的正确路径
我在做什么错? 我要达到的目的是,当用户单击页面上的“ SHOW PHOTOS”按钮时,该页面包含有关特定乐队成员的信息
(此处为site / band_member / member_id),将加载另一个页面(site / band_member / member_id / photos),显示当前成员的所有照片。
DJANGO的新手!!! 谢谢 ! 对不起,我的英语不好:)
答案 0 :(得分:1)
循环中的html不正确。尝试像这样修复它。
<img src="{{ foto.photo_url }}" alt ="{{foto.photo_url }}">
<h5>{{ foto.photo_description }}</h5>