安装django-ckeditor
pip install django-ckeditor
安装Pillow
Pillow是python的一个图像处理库,django-ckeditor需要依赖该库。最简单的安装方法,当然是使用pip,假设你装过pip,可以直接运行以下命令安装:
pip install pillow
配置你的django
1、在你的settings.py文件中,将ckeditor、ckeditor_uploader添加到INATALLED_APPS中。
INSTALLED_APPS = [
'ckeditor',
'ckeditor_uploader',
]
2、setting.py
# media
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
# 配置ckeditor
CKEDITOR_UPLOAD_PATH = 'upload/'
CKEDITOR_CONFIGS = {
'default': {},
'comment_ckeditor': {
'toolbar': 'custom',
'toolbar_custom': [
['Bold', 'Italic', 'Underline', 'Strike', 'Subscript', 'Superscript'],
["TextColor", "BGColor", 'RemoveFormat'],
['NumberedList', 'BulletedList'],
['Link', 'Unlink'],
["Smiley", "SpecialChar", 'Blockquote'],
],
'width': 'auto',
'height': '180',
'tabSpaces': 4,
'removePlugins': 'elementspath',
'resize_enabled': False,
}
}
3、models.py
class Blog(models.Model, ReadNumExpandMethod):
title = models.CharField(max_length=50,verbose_name="标题")
blog_type = models.ForeignKey(BlogType, on_delete=models.CASCADE,verbose_name="分类")
content = RichTextUploadingField()
author = models.ForeignKey(User, on_delete=models.CASCADE,verbose_name="作者")
read_details = GenericRelation(ReadDetail)
created_time = models.DateTimeField(auto_now_add=True,verbose_name="创建时间")
last_updated_time = models.DateTimeField(auto_now=True,verbose_name="上次更新时间")
4、urls.py
urlpatterns = [
path('ckeditor', include('ckeditor_uploader.urls')),
]
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)