首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >flask 富文本编辑器(flask 22)

flask 富文本编辑器(flask 22)

作者头像
用户5760343
发布2019-08-13 14:30:45
发布2019-08-13 14:30:45
9460
举报
文章被收录于专栏:sktjsktj

基于文件上传页面 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 %}

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019.08.09 ,如有侵权请联系 cloudcommunity@tencent.com 删除
目录
  • 基于文件上传页面 flask 21
  • app.config['CKEDITOR_HEIGHT/WIDTH']
  • form.py
  • templates/ckeditor.html
  • post.html
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档