CKEditor 5 for Django

0.2.20 · active · verified Thu Apr 16

django-ckeditor-5 is a Django application that seamlessly integrates the modern CKEditor 5 rich text editor into Django projects, offering an intuitive content creation experience. It provides features like text formatting, image uploads, tables, and code blocks, making it highly customizable. This package is specifically for CKEditor 5, distinct from the older `django-ckeditor` which uses CKEditor 4. The current version is 0.2.20, with a generally active release cadence.

Common errors

Warnings

Install

Imports

Quickstart

To quickly integrate `django-ckeditor-5`, first add `django_ckeditor_5` to your `INSTALLED_APPS` and define `MEDIA_URL`, `MEDIA_ROOT`, and `CKEDITOR_5_CONFIGS` in your `settings.py`. Then, use `CKEditor5Field` in your Django models for rich text fields and `CKEditor5Widget` in your forms. Ensure `{{ form.media }}` is included in your templates for the editor to render correctly.

import os
from django.db import models
from django.forms import ModelForm
from django.conf import settings
from django_ckeditor_5.fields import CKEditor5Field
from django_ckeditor_5.widgets import CKEditor5Widget

# settings.py additions (example)
# INSTALLED_APPS = [
#     ...,
#     'django_ckeditor_5',
# ]
# MEDIA_URL = '/media/'
# MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
# CKEDITOR_5_CONFIGS = {
#     'default': {
#         'toolbar': ['heading', '|', 'bold', 'italic', 'link', 'bulletedList', 'numberedList', 'blockQuote', 'imageUpload', 'fileUpload'],
#         'language': 'en',
#         'width': 'auto'
#     }
# }

class Article(models.Model):
    title = models.CharField(max_length=200)
    content = CKEditor5Field(verbose_name='Rich Content', config_name='default')

    def __str__(self):
        return self.title

class ArticleForm(ModelForm):
    class Meta:
        model = Article
        fields = '__all__'
        widgets = {
            'content': CKEditor5Widget(config_name='default')
        }

# In a Django template, if using a form:
# {% extends 'base.html' %}
# {% block content %}
#   <form method="POST">
#     {{ form.media }}  <!-- IMPORTANT: Don't forget this! -->
#     {% csrf_token %}
#     {{ form.as_p }}
#     <button type="submit">Submit</button>
#   </form>
# {% endblock content %}

view raw JSON →