# 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 here),另一个页面加载(site / band_member / member_id / photos),显示当前成员的所有照片。
DJANGO新!!!谢谢 !对不起我的'坏英语'语言:)
循环中的html不正确。尝试修复这样的。
<img src="{{ foto.photo_url }}" alt ="{{foto.photo_url }}">
<h5>{{ foto.photo_description }}</h5>