Learn how to use add Rich Text Field in Django. In this tutorial, you will learn how to add the rich text field in the Django project. In models, we will use the CK editor fields as Rich Text Field in Django. The CK editor is an external package for Django. This package will allow the addition of Custom fields in Django projects.

Requirements :

pip install django
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. Link for CKEditor

Firstly Create a Django Project :

python -admin django startproject project

This command will create new project.

Navigate to Project folder :

cd project #projectname 

This will change the current directory of the terminal to the project directory

Secondly Creating app with Demo app :

py manage.py startapp demo

Create a demo app for the Django project we have created.

Thirdly Create Urls.py in Demo app

from django.urls import path
from . import views
urlpatterns = [
    path('', views.home, name="home")
]

Here, We are creating the routing paths for the home view this will render out the Html page we have created for the model form. However the ” will represent the localhost path it will render the Html template as the homepage for the project on the localhost address. i.e. 127.0.0.1:8000.

Configure in Settings.py

# Application definition

INSTALLED_APPS = [
    'demo',
    'ckeditor',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]

Model Creation in Demo app

from django.db import models
from ckeditor.fields import RichTextField 

# Create your models here.

class Student(models.Model):
    name = models.CharField(max_length=200)
    remark = RichTextField()

Here, We are creating the Student model in the demo app with two fields name and remark. For name field we are using the basic models package of Django and for the remark field we are using the Rich Text Field from the ck editor fields which we have imported.

Register model in admin page :

from django.contrib import admin
from .models import *
# Register your models here.
admin.site.register(Student)

Configuring the admin site by registering the Student model in the demo app inside the admin.py file. This will make changes to the admin interface and resemble the Student model into the admin site.

Finally Create Superuser and migrate Table :

py manage.py createsuperuser
py manage.py makemigrations
py managde.py migrate

Type the above commands the first command will create a superuser in several steps asking you credentials through the command prompt. The next 2 and 3 commands will make changes to the model and migrate the table to the database and admin page.

Output :

Output