DEV Community

Muminur Rahman
Muminur Rahman

Posted on • Originally published at Medium

4

How To Integrate CKEditor with Django Admin

Sometimes we have to add WYSIWYG editor to an input box in the Django Admin. It is a simple and easy task but may seem tricky for newbies. So, we are going to add a WYSIWYG (which s an acronym for “What You See Is What You Get”) editor to our Django Admin.
For this tutorial, I am assuming that we have a ProjectUpdate model where we need to integrate a WYSIWYG editor. I am using CKEditor 5 for this tutorial.

Add an id to Admin Textarea

At first, we need to add id to the textarea in Django Admin so that we can find it through the id and bind it with CKEditor. For that, we will use formfield_overrides that provides a quick-and-dirty way to override some of the field options on Django admin.
# myproject/admin.py
class ProjectUpdateAdmin(admin.ModelAdmin):
formfield_overrides = {
models.TextField: {'widget': Textarea(
attrs={'id': 'project_update_textarea'})}
}

Here we have added an id project_update_textarea to every textfield in Django admin panel of the ProjectUpdate model.

Add the script to base.html

At first, make a directory named admin in your templates directory and copy the base.html file there. The admin base template can be found in this path: django/contrib/admin/templates/admin/base.html. After that, add these lines before the body closing tag:

# templates/admin/base.httemplates/admin/base.httemplates/admin/base.html
<!-- Start custom scripts for customizing admin form -->
<script src="{% static 'vendor/ckeditor5/ckeditor.js' %}"></script>
<script>
ClassicEditor
.create( document.querySelector( '#project_update_textarea' ) )
.catch( error => {
console.error( error );
} );
</script>
<!-- End custom scripts for customizing admin form -->

I have downloaded the CKEditor package and stored in static/vendor/ckeditor5 directory. Hence, I have used the static tag in the script. You can use the CDN link also if you prefer.

Conclusion

That’s it. Go to your Django Admin Panel. The CKEditor should work now.

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

AWS Q Developer image

Your AI Code Assistant

Automate your code reviews. Catch bugs before your coworkers. Fix security issues in your code. Built to handle large projects, Amazon Q Developer works alongside you from idea to production code.

Get started free in your IDE

👋 Kindness is contagious

Engage with a wealth of insights in this thoughtful article, valued within the supportive DEV Community. Coders of every background are welcome to join in and add to our collective wisdom.

A sincere "thank you" often brightens someone’s day. Share your gratitude in the comments below!

On DEV, the act of sharing knowledge eases our journey and fortifies our community ties. Found value in this? A quick thank you to the author can make a significant impact.

Okay