I am working on a web app in Python (using the Flask framework). I have a post and photo table connected via ForeignKey, my problems:
1. how can i return an empty image if image is empty, and if not empty
2. how do i select just one image from the list of images.
Here is the model:
class Photos(db.Model):
__tablename__ = 'photos'
id = db.Column(db.Integer, primary_key = True)
postid = db.Column(db.Integer, db.ForeignKey('post.id'))
filename = db.Column(db.String)
class Posts(db.Model):
__tablename__ = 'post'
id = db.Column(db.Integer, primary_key = True)
title = db.Column(db.String(30))
photos = db.relationship('Photos', backref='user', lazy='dynamic')
created_at = db.Column(db.DateTime, default=db.func.now())
status = db.Column(db.SmallInteger, default=1)
def thumb(self):
"""
Get post photos
"""
if self.photos is None:
return '/static/images/empty.png'
else:
for p in self.photos:
return '/static/images/%s' % (p.filename)
Usage(the View):
For single image (doesn't work):{{post.thumb()}}
Loop through images (this works):
{% for photo in post.photos %}
<img src="/static/images/{{photo.filename}}"/>
{% endfor %}