Flask Admin Ckeditor图像上传

时间:2018-05-27 19:18:36

标签: python flask ckeditor ckeditor4.x flask-admin

对于Flask Admin如何使用编辑器在文本区域上传图像?

我需要比这更简单的版本:

https://github.com/greyli/flask-ckeditor

1 个答案:

答案 0 :(得分:1)

from flask_ckeditor import *
csrf = CSRFProtect(app)
csrf.init_app(app)

ckeditor = CKEditor(app)

app.config['CKEDITOR_SERVE_LOCAL'] = False
app.config['CKEDITOR_HEIGHT'] = 400
app.config['CKEDITOR_FILE_UPLOADER'] = 'upload'
app.config['UPLOADED_PATH'] = os.path.join(basedir, 'uploads')

@app.route('/files/<filename>')
@csrf.exempt
def uploaded_files(filename):
    path = app.config['UPLOADED_PATH']
    return send_from_directory(path, filename)

import uuid

@app.route('/upload', methods=['POST'])
@csrf.exempt
def upload():
    f = request.files.get('upload')
    extension = f.filename.split('.')[1].lower()
    if extension not in ['jpg', 'gif', 'png', 'jpeg']:
        return upload_fail(message='Image only!')
    unique_filename = str(uuid.uuid4())
    f.filename = unique_filename + '.' + extension
    f.save(os.path.join(app.config['UPLOADED_PATH'], f.filename))
    url = url_for('uploaded_files', filename=f.filename)
    return upload_success(url=url)

在flask admin中的edit.html中

<script>
    CKEDITOR.plugins.addExternal( 'filebrowser', '/static/ckeditor/filebrowser/', 'plugin.js' );
    CKEDITOR.config.extraPlugins = 'filebrowser';
    CKEDITOR.config.filebrowserBrowseUrl  = '/upload';
<script>

这适用于烧瓶管理员上传图像

https://github.com/greyli/flask-ckeditor我用了这个