html编辑器 flask,flask 富文本编辑器(flask 22)

博客介绍了基于Flask集成CKEditor实现文件上传的方法。包含Flask应用配置,如设置CKEditor本地服务、文件上传器等,还给出了路由函数、表单类以及HTML模板的代码示例,实现了富文本表单的提交和文件上传功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

基于文件上传页面 flask 21

6282311281fe

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 %}

Integrate CKEditor

with Flask-CKEditor

{{ form.csrf_token }}

{{ form_field(form.title) }}

{{ form_field(form.body) }}

{{ form.submit }}

{% 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 %}

{{ title }}

{{ body|safe }}

{% endblock %}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值