富文本编辑器是用来提交大段文字的有效途径,同时也比较美观一些。下面记录下最简用法。首先是pip
pip install flask-ckeditor
安装完毕后,观察下面语句,先引入模块然后绑到app上最后就可以个调用了,最简单的取数据方法是:request.form.get(‘ckeditor’)这个就能直接拿出提交数据
from flask_ckeditor import CKEditor
ckeditor = CKEditor(app)
@app.route('/fwb', methods=['GET', 'POST'])
def fwb():if request.method == 'POST':print(request.form.get('ckeditor'))return request.form.get('ckeditor')return render_template('fuwenben.html')
配套的HTML,观察一下就会发现灰常的简单。
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>Title</title>
</head><body>
<form method="post">{{ ckeditor.create() }}<input type="submit">
</form>
{{ ckeditor.load() }}
{{ ckeditor.config() }} <!-- 这时不用设置name参数 -->
</body></html>
另外一种方式是用像wtf一样使用
from flask_wtf import FlaskForm
from flask_ckeditor import CKEditorField
from wtforms import StringField, SubmitField
# 定义类
class PostForm(FlaskForm):title = StringField('Title')body = CKEditorField('Body')submit = SubmitField('Submit')
#定义路由处理
@app.route('/fwbwtf', methods=['GET', 'POST'])
def fwb_wtf():form=PostForm()if request.method == 'POST':print(form.body.data)return form.body.datareturn render_template('fuwenbenwft.html',form=form)
配套HTML
{% extends "bootstrap/base.html" %}
{% block title %}This is an example page{% endblock %}
{% block content %}{{ super() }}
<form method="post">{{ form.title.label }}{{ form.title() }}{{ form.body.label }}{{ form.body() }}{{ form.submit() }}
</form>
{{ ckeditor.load() }}
{{ ckeditor.config(name='body') }}
{% endblock %}