如何在Flask
中显示上载的图像,我在database
中存储路径,并使用Flask-Uploads
将图像作为附件图像和我的上载文件夹uploaddir
上载到文件夹中与应用程序文件夹,那是我想要的
app.py
photos = UploadSet('photos', IMAGES)
def create_app():
app = Flask(__name__, static_folder='templates/static', template_folder='templates')
app.config.from_object(Config)
app.config['UPLOADED_PHOTOS_DEST']
和route.py保存图像
filename = photos.save(request.files['stud_img'], 'student/{}'.format(student.code))
file_desc = _('Personal Image')
student_photo = UploadFile(file_name=filename, file_desc=file_desc, student_id=student.id)
和route.py用于显示
@student_bp.route('/view/<int:student_id>', methods=['GET' ,'POST'])
def view(student_id):
student = Student.query.get(student_id)
img =student.uploadfiles.filter(UploadFile.file_desc == 'pi').first()
return render_template('view_student.html', title=_('View Student'), student_id=student_id, student=student, img=img, )
和我的模板
<div id="crop-avatar">
<!-- Current avatar -->
<img class="img-responsive avatar-view" src="{{ img.file_name }}"
alt="Avatar" title="Change the avatar">
</div>
img.file_name
是正确的,并获得了string
的名称,但我尝试手动放置未显示的图像路径。