Django CKEditor

django-ckeditor интегрирует популярный WYSIWYG редактор CKEditor в Django.

Установка

1# Установка пакета
2pip install django-ckeditor
1# Или через Poetry
2poetry add django-ckeditor

Настройка

 1# settings.py
 2INSTALLED_APPS = [
 3    'ckeditor',
 4    'ckeditor_uploader',
 5]
 6
 7# Настройки CKEditor
 8CKEDITOR_UPLOAD_PATH = "uploads/"
 9CKEDITOR_CONFIGS = {
10    'default': {
11        'toolbar': 'full',
12        'height': 300,
13        'width': '100%',
14    },
15}

Использование в моделях

1from ckeditor.fields import RichTextField
2
3class Article(models.Model):
4    title = models.CharField(max_length=200)
5    content = RichTextField()
6    created_at = models.DateTimeField(auto_now_add=True)

В формах

1from ckeditor.widgets import CKEditorWidget
2
3class ArticleForm(forms.ModelForm):
4    content = forms.CharField(widget=CKEditorWidget())
5
6    class Meta:
7        model = Article
8        fields = ['title', 'content']

Кастомизация toolbar

 1# settings.py
 2CKEDITOR_CONFIGS = {
 3    'default': {
 4        'toolbar': 'Custom',
 5        'toolbar_Custom': [
 6            ['Bold', 'Italic', 'Underline'],
 7            ['NumberedList', 'BulletedList', '-', 'Outdent', 'Indent'],
 8            ['JustifyLeft', 'JustifyCenter', 'JustifyRight', 'JustifyBlock'],
 9            ['Link', 'Unlink'],
10            ['RemoveFormat', 'Source']
11        ],
12        'height': 400,
13        'width': '100%',
14        'removePlugins': 'stylesheetparser',
15        'allowedContent': True,
16    },
17}

FAQ

Q: Как кастомизировать toolbar?
A: Настрой CKEDITOR_CONFIGS с нужными опциями toolbar.

Q: Можно ли загружать изображения?
A: Да, используй ckeditor_uploader для загрузки файлов.