在django项目中使用ckeditor

1、安装ckeditor


pip install django-ckeditor
pip install pillow

 

2、配置

 

a,在你的settings.py文件中

INSTALLED_APPS = [
'ckeditor', 
'ckeditor_uploader' 
]

b,在你的settings.py文件中,添加CKEDITOR_UPLOAD_PATH配置项

MEDIA_URL = "/media/"
MEDIA_ROOT = os.path.join(BASE_DIR, "media")
CKEDITOR_UPLOAD_PATH = "images"

c.在settings目录中增加如下配置即可
CKEDITOR_CONFIGS = {
'default': {
'toolbar': (
['div','Source','-','Save','NewPage','Preview','-','Templates'],
['Cut','Copy','Paste','PasteText','PasteFromWord','-','Print','SpellChecker','Scayt'],
['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'],
),
}
}

 

3、使用

 

在models.py中引用

from ckeditor.fields import RichTextField
from ckeditor_uploader.fields import RichTextUploadingField

创建模型时间:

profile = RichTextField(verbose_name='活动简介')

这样在admin中直接就可以显示了。

 

4、在模板中使用富文本编辑器

 

需要引用:
<script src="{% static 'ckeditor/ckeditor-init.js' %}"></script>
<script src="{% static 'ckeditor/ckeditor/ckeditor.js' %}"></script>
如果是显示富文本的内容,记得加上safe过滤器哟。

posted @ 2020-10-27 09:17  python小童鞋  阅读(154)  评论(0)    收藏  举报