如何在django项目中使用django-ckeditor

1、安装

  pip install django-ckeditor

2、安装Pillow

  Pillow是python的一个图像处理库,django-ckeditor需要依赖该库。最简单的安装方法,当然是使用pip,假设你装过pip,可以直接运行以下命令安装:

  pip install pillow

3、配置Django

  1、在settings.py文件中,将ckeditor、ckeditor_uploader添加到INATALLED_APPS中。

  2、在settings.py文件中,添加CKEDITOR_UPLOAD_PATH配置项。

    CKEDITOR_UPLOAD_PATH = "article_images"

4、使用CKeditor

  from django.db import models

  from ckeditor.fields import RichTextField

  # Create your models here.

  class Goods(models.Model):
    content = RichTextField(verbose_name="商品详情")

  启动应用,富文本编辑器效果如下图所示:


  如果感觉可以用的工具比较少,那么可以在settings里增加如下配置即可。

  # 配置CKeditor
  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'],
      ),
    }
  }

  现在一个完美的富文本输入框就完成了!!!

原文地址:https://www.cnblogs.com/Nxx-clara/p/11084720.html