想关代码:
配置:
CKEDITOR_HEIGHT = 600
CKEDITOR_WIDTH = 600
CKEDITOR_FILE_UPLOADER = ‘/upload/’
CKEDITOR_ENABLE_CSRF = True
表单:
class Blogcomment(FlaskForm):
title = StringField(‘标题’)
category = StringField(‘类别’)
text = CKEditorField(‘Body’)
submit = SubmitField(‘提交’)
接收图片的类:
class PictureHandler(MethodView):
def post(self):
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!’) # 返回upload_fail调用
img_url = put_file(f)
return img_url
process_file.add_url_rule(‘/upload/’, view_func=PictureHandler.as_view(‘picturehandler’))
页面:
{% block content %}
新增文章
{{ form.csrf_token }}
{{ form.title.label }}{{ form.title() }}
{{ form.category.label }}{{ form.category() }}
{{ form.text() }}
{{ form.submit() }}
{% block scripts %}
{{ super() }}
{{ ckeditor.load() }}
{{ ckeditor.config(name='text') }}
{% endblock %}
{% endblock %}