Визуальный редактор CKEditor
Подробная инструкция по установке
Выполняем пошаговую установку:
- Для начала нужно установить CKEditor
pip install django-ckeditor
- Нужно добавить наше приложение в файл
settings.py
вINSTALLED_APPS
:'ckeditor',
- Выполняем
./manage.py collectstatic
(соглашаемся с тем что файлы возможно будут перезаписаны) - 4-й пункт можем пропустить
Настраиваем возможность загрузки файлов и изображений через CKEditor
- Добавляем приложение в файл
settings.py
вINSTALLED_APPS
:'ckeditor_uploader',
- Добавляем константу в файл
settings.py
:CKEDITOR_UPLOAD_PATH = "uploads/"
чтобы указать где сохранять файлы - Пропускаем
- Добавляем следующий параметр в список URL адресов в корневом файле
urls.py
:
path('ckeditor/', include('ckeditor_uploader.urls')),
Встраивание виджета CKEditor
В файле admin.py
импортируем:
from ckeditor_uploader.widgets import CKEditorUploadingWidget
если мы хотим иметь возможность загрузки. Если не хотим, импортируем следующую библиотеку:
from ckeditor.widgets import CKEditorWidget
И добавляем класс:
class NewsAdminForm(forms.ModelForm):
content = forms.CharField(widget=CKEditorUploadingWidget()) # Поле моделей у нас назвается 'content', поэтому оставляем переменную без именений
class Meta:
model = News # Тут нужно указать название можеди в которой мы будем использовать CKEditor
fields = '__all__'
Затем в класс в admins.py
, в котором мы будем использовать CKEdtotor добавляем:
form = NewsAdminForm
Для кастомизации CKEditor добавляем настройки в файл settings.py
CKEDITOR_CONFIGS = {
'default': {
'skin': 'moono',
# 'skin': 'office2013',
'toolbar_Basic': [
['Source', '-', 'Bold', 'Italic']
],
'toolbar_YourCustomToolbarConfig': [
{'name': 'document', 'items': ['Source', '-', 'Save', 'NewPage', 'Preview', 'Print', '-', 'Templates']},
{'name': 'clipboard', 'items': ['Cut', 'Copy', 'Paste', 'PasteText', 'PasteFromWord', '-', 'Undo', 'Redo']},
{'name': 'editing', 'items': ['Find', 'Replace', '-', 'SelectAll']},
{'name': 'forms',
'items': ['Form', 'Checkbox', 'Radio', 'TextField', 'Textarea', 'Select', 'Button', 'ImageButton',
'HiddenField']},
'/',
{'name': 'basicstyles',
'items': ['Bold', 'Italic', 'Underline', 'Strike', 'Subscript', 'Superscript', '-', 'RemoveFormat']},
{'name': 'paragraph',
'items': ['NumberedList', 'BulletedList', '-', 'Outdent', 'Indent', '-', 'Blockquote', 'CreateDiv', '-',
'JustifyLeft', 'JustifyCenter', 'JustifyRight', 'JustifyBlock', '-', 'BidiLtr', 'BidiRtl',
'Language']},
{'name': 'links', 'items': ['Link', 'Unlink', 'Anchor']},
{'name': 'insert',
'items': ['Image', 'Flash', 'Table', 'HorizontalRule', 'Smiley', 'SpecialChar', 'PageBreak', 'Iframe']},
'/',
{'name': 'styles', 'items': ['Styles', 'Format', 'Font', 'FontSize']},
{'name': 'colors', 'items': ['TextColor', 'BGColor']},
{'name': 'tools', 'items': ['Maximize', 'ShowBlocks']},
{'name': 'about', 'items': ['About']},
'/', # put this to force next toolbar on new line
{'name': 'yourcustomtools', 'items': [
# put the name of your editor.ui.addButton here
'Preview',
'Maximize',
]},
],
'toolbar': 'YourCustomToolbarConfig', # put selected toolbar config here
# 'toolbarGroups': [{ 'name': 'document', 'groups': [ 'mode', 'document', 'doctools' ] }],
# 'height': 291,
# 'width': '100%',
# 'filebrowserWindowHeight': 725,
# 'filebrowserWindowWidth': 940,
# 'toolbarCanCollapse': True,
# 'mathJaxLib': '//cdn.mathjax.org/mathjax/2.2-latest/MathJax.js?config=TeX-AMS_HTML',
'tabSpaces': 4,
'extraPlugins': ','.join([
'uploadimage', # the upload image feature
# your extra plugins here
'div',
'autolink',
'autoembed',
'embedsemantic',
'autogrow',
# 'devtools',
'widget',
'lineutils',
'clipboard',
'dialog',
'dialogui',
'elementspath'
]),
}
}