Django学习18 -- 富文本编辑器(Rich Edit)
1. 富文本编辑器,Rich Edit
CharField,单行文本(Django默认)
TextField,多行文本(Django默认)
富文本编辑器是能显示(编辑)图文效果的输入控件
2. Django支持的富文本编辑器
CKEditor
-即FCKeditor(文本编辑器),一款功能强大的开源在线文本编辑器,所见即所得。基于 JavaScript 开发,在 Web 上实现类似于 Microsoft Word 的强大的功能,不必在客户端安装,兼容各大主流浏览器。参考,
-安装ckeditor:pip install django-ckeditor
-安装js_asset:pip install django-js-asset
-注册 ckeditor
INSTALLED_APPS = [
......
'ckeditor', # rick edit
......
]
-配置 Configuration -- All editor features
// This is actually the default value.
config.toolbar_Full =
[
{ name: 'document', items : [ 'Source','-','Save','NewPage','DocProps','Preview','Print','-','Templates' ] },
{ name: 'clipboard', items : [ 'Cut','Copy','Paste','PasteText','PasteFromWord','-','Undo','Redo' ] },
{ name: 'editing', items : [ 'Find','Replace','-','SelectAll','-','SpellChecker', 'Scayt' ] },
{ name: 'forms', items : [ 'Form', 'Checkbox', 'Radio', 'TextField', 'Textarea', 'Select', 'Button', 'ImageButton', 'HiddenField' ] },
'/',
{ name: 'basicstyles', items : [ 'Bold','Italic','Underline','Strike','Subscript','Superscript','-','RemoveFormat' ] },
{ name: 'paragraph', items : [ 'NumberedList','BulletedList','-','Outdent','Indent','-','Blockquote','CreateDiv','-','JustifyLeft','JustifyCenter','JustifyRight','JustifyBlock','-','BidiLtr','BidiRtl' ] },
{ name: 'links', items : [ 'Link','Unlink','Anchor' ] },
{ name: 'insert', items : [ 'Image','Flash','Table','HorizontalRule','Smiley','SpecialChar','PageBreak' ] },
'/',
{ name: 'styles', items : [ 'Styles','Format','Font','FontSize' ] },
{ name: 'colors', items : [ 'TextColor','BGColor' ] },
{ name: 'tools', items : [ 'Maximize', 'ShowBlocks','-','About' ] }
];
-settings.py设置
## CKEDITOR_CONFIGS = {......}
## ...\ckeditor\configs.py
DEFAULT_CONFIG = {
"skin": "moono-lisa",
"toolbar_Basic": [["Source", "-", "Bold", "Italic"]],
"toolbar_Full": [
[
"Styles",
"Format",
"Bold",
"Italic",
"Underline",
"Strike",
"SpellChecker",
"Undo",
"Redo",
],
["Link", "Unlink", "Anchor"],
["Image", "Flash", "Table", "HorizontalRule"],
["TextColor", "BGColor"],
["Smiley", "SpecialChar"],
["Source"],
],
"toolbar": "Full",
"height": 291,
"width": 835,
"filebrowserWindowWidth": 940,
"filebrowserWindowHeight": 725,
}
-示例
![](https://img-blog.csdnimg.cn/img_convert/6b36639667b5fa5e9a327c45e9698409.png)
![](https://img-blog.csdnimg.cn/img_convert/8253ad4211263e1b611f6f856c26c99a.png)
-参考
*CKEditor 3 JavaScript API Documentation
WangEditor
-国人开发的一款非常优秀的富文本编辑器,开源 Web 富文本编辑器
-安装:pip install django-wangeditor
-注册 wangeditor(INSTALLED_APPS)
INSTALLED_APPS = [
......
'wangeditor' # rich edit, wangeditor
......
]
from wangeditor.fields import WangRichTextField
rich_edit_box = WangRichTextField('RichEdit', max_length=1000, )
# Error: cannot import name 'force_text' from 'django.utils.encoding'
# Solution:settings.py添加
import django
from django.utils.encoding import force_str
django.utils.encoding.force_text = force_str
-参考
-示例
![](https://img-blog.csdnimg.cn/img_convert/56d33b1f836d9f987e5a88c2be1437b2.png)
DjangoUeditor -- Python3 / Django 4支持不好,不能正常调试
-百度开源的在线HTML编辑器
-安装:pip install djangoueditor
-编译错误
# from widgets import UEditorWidget,AdminUEditorWidget
# ModuleNotFoundError: No module named 'widgets'
# 只支持python 2.7, 不支持python 3.X。需要从Git Hub下载最新3版本
# DjangoUeditor3 -- https://github.com/twz915/DjangoUeditor3
-- https://gitcode.net/mirrors/twz915/djangoueditor3?utm_source=csdn_github_accelerator
-- https://blog.csdn.net/m0_67138207/article/details/125028085
-pip install django-ueditor(什么东东?)
from django.utils.encoding import smart_text
# Django 4.0中删除功能,django.utils.encoding.force_text() 和 smart_text() 被移除
# INSTALLED_APPS,设置并替换 smart_text -> smart_str
import django
from django.utils.encoding import smart_str
django.utils.encoding.smart_text = smart_str
TinyMCE -- Python3 / Django 4支持不好,不能正常调试
-可视化HTML编辑器。参考,
-安装:pip install django-tinymce(pip install -i https://pypi.tuna.tsinghua.edu.cn/simple xx)
-注册 tinymce(INSTALLED_APPS)
-ERROR
>> Issue 1: django.core.urlresolvers -> django.urls
# from django.core.urlresolvers import reverse
# ModuleNotFoundError: No module named 'django.core.urlresolvers'
# django2.0 将 django.core.urlresolvers 包 更改为了 django.urls 包
# ...\tinymce\views.py
##from django.core import urlresolvers
from django import urls
# ...\tinymce\widgets.py
##from django.core.urlresolvers import reverse
from django.urls import reverse
>> Issue 2: flatatt
# ...\tinymce\widgets.py
##from django.forms.widgets import flatatt
ImportError: cannot import name 'flatatt' from 'django.forms.widgets'
>> Issue 3: baseurl
# ...\tinymce\settings.py
##from utils import baseurl
from tinymce.utils import baseurl
-参考:
3. 文件参考:
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple
相关文章
- Django学习笔记一初识Django
- 最简单的 Django 教程
- Django(31)模板中常用的过滤器
- django官方入门教程_DJango
- 【Django】在大型项目中的django的性能模型字段primary_key
- python 和 django 学习资料收集(2020-4-25 更新)
- Django学习
- django 1.8 自定义模板标签(simple_tag)和过滤器(filter)
- pycharm2019.2 专业版 导入及配置已存在的 Django 项目
- django import_export 个性化可视化导出外键字段
- django import_export export 导出时新增自定义字段
- python-Django 模型层-模型层示例
- 用django写个CMS系统详解大数据
- Django的Models(二)映射关系详解大数据
- Django的Rbac介绍2详解编程语言
- Django的CBV的学习详解编程语言
- Django的模板继承详解编程语言
- 深入浅出Django与MSSQL整合(django-mssql)
- Linux系统下部署Django网站快速指南(linux 部署django)