简体   繁体   中英

Flask-admin display image with path from column

I have my DB set like this :

  MediaPath    DateCreated    Message   Type
/home/1/1.jpg   21.09.2017              jpeg
/home/2/2.jpg   ....          ....      ....

How can i set up Flask code to preview images from the MediaPath column without uploading them to /static/ folder? This is usual flask-admin code template:

class ImageView(sqla.ModelView):
def _list_thumbnail(view, context, model, name):
    if not model.path:
        return ''

    return Markup('<img src="%s">' % url_for('static',
                                             filename=form.thumbgen_filename(model.path)))

column_formatters = {
    'path': _list_thumbnail
}


form_extra_fields = {
    'path': form.ImageUploadField('Image',
                                  base_path=file_path,
                                  thumbnail_size=(100, 100, True))
}

Is the path for the files in the MeidaPath column their absolute path?

Make sure that the path returned by form.thumbgen_filename(model.path) is the absolute path for the file and you can do the following:

Instead of:

return Markup('<img src="%s">' % url_for('static', filename=form.thumbgen_filename(model.path)))

Use:

return Markup('<img src="%s">' % form.thumbgen_filename(model.path))

** Assuming that the method thumbgen_filename() returns the absolute path to the image thumbnail (you haven't posted the code for that method so I assuming that is what the method returns)**

When you use url_for('static', filename='some-path') it generate a URL that includes the static path and the files that are inside the static folder depending on the value you provide to url_for's filename argument. If want to load your files using their absolute path, just provide their absolute path without using url_for()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM