如何在 Flask Admin 中上传多张图片

How to upload multiple images in flask Admin

我正在使用 flask 创建一个博客网站,init admin 想为每个博客添加多张照片,因为它是唯一想要上传照片的管理员,因此我使用 flask admin 创建新博客并做所有他填充管理员的事情想要,但我遇到了一个问题,即如何添加为每个博客添加多张照片的功能我已经阅读了 Flask 管理文档,但无法为添加多张图片做任何事情,但我确实找到了添加单张照片和一个我的代码示例是 here

但这又不是管理员想要的

请帮帮我, 谢谢 代码:

import os
import os.path as op

from flask import Flask, url_for
from flask_sqlalchemy import SQLAlchemy
from wtforms import fields

from sqlalchemy.event import listens_for
from jinja2 import Markup

from flask_admin import Admin, form
from flask_admin.form import rules
from flask_admin.contrib import sqla


# Create application
app = Flask(__name__, static_folder='files')

app.config['FLASK_ADMIN_SWATCH'] = 'cerulean'

# Create dummy secrey key so we can use sessions
app.config['SECRET_KEY'] = '123456790'

# Create in-memory database
app.config['DATABASE_FILE'] = 'sample_db.sqlite'
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + app.config['DATABASE_FILE']
app.config['SQLALCHEMY_ECHO'] = True
db = SQLAlchemy(app)


# Create directory for file fields to use
file_path = op.join(op.dirname(__file__), 'static/images')
try:
    os.mkdir(file_path)
except OSError:
    pass


class Blog(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.Unicode(64))
    path = db.Column(db.Unicode(128))

    def __unicode__(self):
        return self.name


@listens_for(Blog, 'after_delete')
def del_image(mapper, connection, target):
    if target.path:
        # Delete image
        try:
            os.remove(op.join(file_path, target.path))
        except OSError:
            pass

        # Delete thumbnail
        try:
            os.remove(op.join(file_path,
                              form.thumbgen_filename(target.path)))
        except OSError:
            pass

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
    }

    # Alternative way to contribute field is to override it completely.
    # In this case, Flask-Admin won't attempt to merge various parameters for the field.
    form_extra_fields = {
        'path': form.ImageUploadField('Image',
                                      base_path=file_path,
                                      thumbnail_size=(200, 300, True))
    }


# Flask views
@app.route('/')
def index():
    return '<a href="/admin/">Click me to get to Admin!</a>'

# Create admin
admin = Admin(app, 'Example: Forms', template_mode='bootstrap3')

# Add views
admin.add_view(ImageView(Blog, db.session))


def build_sample_db():
    """
    Populate a small db with some example entries.
    """

    db.drop_all()
    db.create_all()


    db.session.commit()
    return


if __name__ == '__main__':

    # Build a sample db on the fly, if one does not exist yet.
    app_dir = op.realpath(os.path.dirname(__file__))
    database_path = op.join(app_dir, app.config['DATABASE_FILE'])
    if not os.path.exists(database_path):
         build_sample_db()

    # Start app
    app.run(debug=True)

p.s 这是我在堆栈中的第一个问题,如果问题有任何问题,我很抱歉

是的,我正在回答我自己的问题,因为我找到了一个

哦,所以这是一个文件 feilds.py 只需将其添加到您的文件夹中,以下是使用它的过程

#import class MultipleImageUploadField from feilds.py
from fields import MultipleImageUploadField
import ast
# some more method i used 
from secrets import token_hex
import os.path as op

# Create directory to save images
file_path = op.join(op.dirname(__file__), '../static/images/products') # path
try:
    os.mkdir(file_path)
except OSError:
    pass

# function to change name of each image
def prefix_name(obj, file_data):
    _, ext = op.splitext(file_data.filename)
    return token_hex(10) + ext

# function to delete image and thumbnail when deleting image in edit section
@listens_for(Product, 'after_delete') # change product with db.Model in which you are saving file names
def del_image(mapper, connection, target):
    if target.images:
        # Delete image
        try:
            print(file_path, target.images)
            os.remove(op.join(file_path, target.images))
        except OSError:
            pass

        # Delete thumbnail
        try:
            os.remove(op.join(file_path,
                              form.thumbgen_filename(target.images)))
        except OSError:
            pass
# add this inside your ModelView class
def _list_thumbnail(view, context, model, name):

        if not model.images:
            return ''

        def gen_img(filename):
            return '<img src="{}">'.format(url_for('static', 
                   filename="images/products/" + form.thumbgen_filename(image)))

        return Markup("<br />".join([gen_img(image) for image in ast.literal_eval(model.images)]))

column_formatters = {'images': _list_thumbnail} # My column name is images in this case

form_extra_fields = {'images': MultipleImageUploadField("Images",
                              base_path=file_path,
                              url_relative_path="images/products/", # relative path of your image (you need to give this in order to see thumbnail in edit secction)
                              thumbnail_size=(200, 200, True), # need to pass to make thumbnail
                              namegen=prefix_name) # rename each image

就这些了希望对你有帮助