django中使用ckeditor

普通的富文本编辑

  1. 安装:pip install django-ckeditor

  2. 激活应用'ckeditor'

  3. 然后使用

from ckeditor.fields import RichTextField
content = RichTextField()
加上上传图片
  1. 安装:
pip install django-ckeditor
pip install pillow
  1. 在INSTALL_APP中激活:'ckeditor', 'ckeditor_uploader'

  2. 在settings.py中配置:(完整请参考:https://github.com/django-ckeditor/django-ckeditor)

CKEDITOR_UPLOAD_PATH = 'ckeditor/'
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
  1. urls.py文件:
from django.conf.urls import url, include
from django.conf import settings
from django.conf.urls.static import static

urlpatterns = [
    url(r'^ckeditor/', include('ckeditor_uploader.urls')),
]
urlpatterns += static(settings.MEDIA_URL,document_root=settings.MEDIA_ROOT)
  1. 在models.py文件中的使用。
from ckeditor_uploader.fields import RichTextUploadingField

content = RichTextUploadingField(verbose_name="内容", config_name="other")

注:settings中对文件进行配置
详情请参考:https://github.com/django-ckeditor/django-ckeditor

CKEDITOR_CONFIGS = {
    'default': {
        'toolbar': 'full',
        'extraPlugins': ','.join(['codesnippet', 'uploadimage', 'widget', 'lineutils',]),
    },
    'other':{
         ... 
    }
}
posted @ 2020-09-27 14:18  小默同学  阅读(122)  评论(0编辑  收藏  举报