flask 富文本编辑器(flask 22)

2019-08-13 14:30:45 浏览数 (1)

基于文件上传页面 flask 21

image.png

from flask_ckeditor import CKEditor, upload_success, upload_fail

app = Flask(name) ckeditor=CKEditor(app) app.config['CKEDITOR_SERVE_LOCAL'] = True app.config['CKEDITOR_FILE_UPLOADER'] = 'upload_for_ckeditor' app.config['CKEDITOR_PKG_TYPE'] = 'standard' #basic,full app.config['CKEDITOR_LANGUAGE']='zh-cn'

app.config['CKEDITOR_HEIGHT/WIDTH']

@app.route('/ckeditor', methods=['GET', 'POST']) def integrate_ckeditor(): form = RichTextForm() if form.validate_on_submit(): title = form.title.data body = form.body.data flash('Your post is published!') return render_template('post.html', title=title, body=body) return render_template('ckeditor.html', form=form) @app.route('/upload-ck', methods=['POST']) def upload_for_ckeditor(): f = request.files.get('upload') if not allowed_file(f.filename): return upload_fail('Image only!') f.save(os.path.join(app.config['UPLOAD_PATH'], f.filename)) url = url_for('get_file', filename=f.filename) return upload_success(url, f.filename)

form.py

from flask_ckeditor import CKEditorField class RichTextForm(FlaskForm): title = StringField('Title', validators=[DataRequired(), Length(1, 50)]) body = CKEditorField('Body', validators=[DataRequired()]) submit = SubmitField('Publish')

templates/ckeditor.html

{% extends 'base.html' %} {% from 'macros.html' import form_field %}

{% block content %} <h2>Integrate <a href="https://ckeditor.com">CKEditor</a> with <a href="https://github.com/greyli/flask-ckeditor">Flask-CKEditor</a> </h2> <form method="post"> {{ form.csrf_token }} {{ form_field(form.title) }} {{ form_field(form.body) }} {{ form.submit }} </form> {% endblock %}

{% block scripts %} {{ super() }} {{ ckeditor.load() }} {{ ckeditor.config(name='body') }} {% endblock %}

post.html

{% extends 'base.html' %}

{% block title %}{{ title }}{% endblock %}

{% block head %} {{ super() }} {{ ckeditor.load_code_theme() }} {% endblock %}

{% block content %} <h1>{{ title }}</h1> <p>{{ body|safe }}</p> {% endblock %}

0 人点赞