DEV Community

Digamber Jha
Digamber Jha

Posted on • Originally published at coding-india.herokuapp.com

How to add Rich Text Editor Field in Django?

Sometimes we need a field to get data other than a paragraph or heading tag that field is called the text area field. This field is used to get the long text as input from the user. Or we can say that we use the text area field as an alternative to the input field to get the long input from the user. The Syntax of the text area filed is:

Syntax:

But the text area field is quite simple and we can’t get the option to format the text or change the layout of the text and add images to the text area field. So, we use Rich Text Editors for design purposes.

Rich Text Editor:

The rich-text editor is based on an open-source application called CKEditor. For more information on the CKEditor, you may also refer to the following website.

https://ckeditor.com/docs/

We will use the rich text editor field as the Rich Text field in Django Model. It is an external Package, so we need to download it before implementation.

How to download Rich Text Editor for Django:

For downloading we need to install the following package using the pip command. After Installing we have to add it to the installed app which will placed in project’s folder.

pip install django-ckeditor

The above commands will install the package of the latest Django on your local machine. And ck-editor packages and its built-in files into our local machine.

After installation add it to the Installed app.

Settings.py:

INSTALLED_APPS = [
    'website',
    'ckeditor',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]
Enter fullscreen mode Exit fullscreen mode

After this, we are able it to just import and use the Rich Text Editor Field in Django.

Models.py:

from django.db import models
from ckeditor.fields import RichTextField
class Blogs(models.Model):
    title = models.CharField(max_length=200)
    desc = RichTextField()
Enter fullscreen mode Exit fullscreen mode

After that, we need to do makemigrations and migrate to apply the changes at the database.
And done!
You have successfully installed Rich Text Editor Field in Django.

Some Other Blogs

A Step-By-Step Tutorial For Coding Your First HTML Document
Complete CSS Grid Tutorial | Learn CSS by practicing it
How to deploy django on heroku server?
What is virtual environment and how to use it in python?
What are the file structure of django projects and apps and how to create django project and apps

Top comments (0)