ckeditor提供了个性化的定制编辑器的功能,只需要预先设定toolbar的名称,使用时直接引用即可。
在setting.py文件中加入ckeditor的相关配置,这里我预先定义两个配置名称“desc”和“body”,将来使用的时候直接引用配置名称即可。
另外还可以设置编辑器的背景色,关键字时“uiColor”。
CKEDITOR_CONFIGS = {
'desc': {
'toolbar': (
# ['div','Source','-','Save','NewPage','Preview','-','Templates'],
['Cut','Copy','Paste','PasteText','PasteFromWord'],
# ['Undo','Redo','-','Find','Replace','-','SelectAll','RemoveFormat'],
# ['Form','Checkbox','Radio','TextField','Textarea','Select','Button', 'ImageButton','HiddenField'],
['Bold','Italic','Underline','Strike','-','Subscript','Superscript'],
['NumberedList','BulletedList','-','Outdent','Indent','Blockquote'],
['JustifyLeft','JustifyCenter','JustifyRight','JustifyBlock'],
# ['Link','Unlink','Anchor'],
['Image','Flash','Table','HorizontalRule','Smiley','SpecialChar','PageBreak'],
['Styles','Format','Font','FontSize'],
['TextColor','BGColor'],
# ['Maximize','ShowBlocks','-','About', 'pbckcode'],
['Blockquote', 'CodeSnippet'],
),
'height': 100,
'width': 1200,
'tabSpaces': 4,
'extraPlugins' : 'autogrow', # 代码插件
},
'body': {
# 'toolbar': "Full",
'toolbar': (
['div','Source'],
['Cut','Copy','Paste','PasteText','PasteFromWord','-','SpellChecker','Scayt'],
# ['Undo','Redo','-','Find','Replace','-','SelectAll','RemoveFormat'],
['Styles', 'Format', 'Font', 'FontSize'],
['Bold', 'Italic', 'Underline', ' Strike', '-', 'Subscript', 'Superscript'],
['Form', 'Checkbox', 'Radio', 'TextField', 'Textarea', 'Select', 'Button', 'ImageButton', 'HiddenField'],
['NumberedList', 'BulletedList', '-', 'Outdent','Indent', 'Blockquote'],
['JustifyLeft', 'JustifyCenter', 'JustifyRight', 'JustifyBlock'],
['Link', 'Unlink', 'Anchor'],
['Image', 'Flash', 'Table', 'HorizontalRule', 'Smiley', 'SpecialChar', 'PageBreak'],
['TextColor', 'BGColor'],
['Maximize',' ShowBlocks', '-', 'About', 'pbckcode'],
['Blockquote', 'CodeSnippet'],
),
'height': 600,
'width': 1200,
'tabSpaces': 4,
# 'uiColor': '#CCEAEE',
'extraPlugins': 'codesnippet, autogrow', # 代码插件
'autoGrow_maxHeight ': 1200,
}
}
使用时直接根据定义好的名称直接引用即可:
content = forms.CharField(widget=CKEditorUploadingWidget(config_name='body'), label='正文', required=True)
desc = RichTextField(config_name='desc', verbose_name='摘要')
展示效果如下: