django-ckeditor-5 supported plugins.
django-ckeditor-5 supports the following plugin. You can also add new plugins but we will discuss this upcoming article.
Essentials, UploadAdapter, Autoformat, Bold, Italic, Underline, Strikethrough, Code, Subscript, Superscript, BlockQuote, Heading, Image, ImageCaption, ImageStyle, ImageToolbar, ImageUpload, ImageInsert, ImageResize, Link, List, Paragraph, Alignment, Font, PasteFromOffice, SimpleUploadAdapter, MediaEmbed, RemoveFormat, Table, TableToolbar, TableProperties, TableCellProperties, Indent, IndentBlock, Highlight, TodoList, ListProperties, SourceEditing, GeneralHtmlSupport
CustomStorage in django-ckeditor-5
By default, django-ckeditor-5 saves media files in the media folder but we can override this setting.
# settings.py
# CKEDITOR_5_FILE_STORAGE = "path_to_storage.CustomStorage" # optional
CKEDITOR_5_FILE_STORAGE = "yourapp.storage.CustomStorage"
Create a new file storage.py in your app and add the following code to save media files in the media/uploads/images folder
# storage.py
import os
from urllib.parse import urljoin
from django.conf import settings
from django.core.files.storage import FileSystemStorage
class CustomStorage(FileSystemStorage):
"""Custom storage for django_ckeditor_5 images."""
location = os.path.join(settings.MEDIA_ROOT, "uploads/images/")
base_url = urljoin(settings.MEDIA_URL, "uploads/images/")
Add django-ckeditor-5 in your views and templates.
# forms.py
from django import forms
from django_ckeditor_5.widgets import CKEditor5Widget
from .models import Article
class ArticleForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# it is required to set it False,
# otherwise it will throw error in console
self.fields["text"].required = False
class Meta:
model = Article
fields = '__all__'
# you can also use CKEditor5Widget in your forms.
# we don't need it. because we have model form
# widgets = {
# "text": CKEditor5Widget(
# attrs={"class": "django_ckeditor_5"}, config_name="extends"
# )
# }
# views.py
Dostları ilə paylaş: |