django使用django-ckeditor实现富文本编辑器

安装

pip install django-ckeditor

注册应用

INSTALLED_APPS = [
    ...
    'ckeditor',  # 富文本编辑器
    'ckeditor_uploader',  # 富文本编辑器上传图片模块
    ...
]

配置settings/dev.py

# 富文本编辑器ckeditor配置
CKEDITOR_CONFIGS = {
    'default': {
        # 'toolbar': 'full',  # 工具条功能
        'toolbar': 'Custom',  # 自定义工具条功能,功能名称可以在full模式下去前端查看响应代码,例如图片 cke_button__image_icon会有这么一个类 图片功能名称就是Image(首字母大写)
        'toolbar_Custom': [
            ['Bold', 'Italic', 'Underline', ],
            ['NumberedList', 'BulletedList', '-', 'Outdent', 'Indent', '-', 'JustifyLeft', 'JustifyCenter',
             'JustifyRight', 'JustifyBlock', 'Image'],
            ['Link', 'Unlink'],
            ['RemoveFormat', 'Source']
        ],
        'height': 300,  # 编辑器高度
        # 'width': 300,     # 编辑器宽
    },
}
CKEDITOR_UPLOAD_PATH = ''  # 上传图片保存路径,留空则调用django的文件上传功能

在总路由中添加

path(r'^ckeditor/', include('ckeditor_uploader.urls')),

更改模型中的字段类型

"""
ckeditor提供了两种类型的Django模型类字段
ckeditor.fields.RichTextField` 不支持上传文件的富文本字段
ckeditor_uploader.fields.RichTextUploadingField` 支持上传文件的富文本字段
"""
 brief = RichTextUploadingField(max_length=2048, verbose_name="课程概述", null=True, blank=True)
原文地址:https://www.cnblogs.com/wtil/p/14968593.html