A WYSIWYG (pronounced “wiz-ee-wig”) editor or program is one that allows a developer to see what the result will look like while the interface or document is being created. WYSIWYG is an acronym for “what you see is what you get”.
There are many WYSIWYG editors out there such as TinyMCE, Froala, CKEditor, etc. But recently I stumble on this very simple yet flexible text editor Summernote.
In this tutorial, we will go through the integration of the Summernote WYSIWYG HTML Editor in Django.
But why would you need a WYSIWYG editor?
Suppose you are building a blog application you have to format your content a lot like adding different headings, links, images and lot in such case a WYSIWYG editor can save you from a lot of pain.
Also even if you are not making a blog application, any kind of application needs about us, terms and conditions kind of pages, writing them on WYSIWYG is comparatively faster.
Integrating Summernote In Django
Considering the scope of the article I am assuming you already have a basic Django project up and running if not reading the following articles is recommended.
In order to make the development server i.e. DEBUG=True serve the media files add the following configuration to url.py.
from django.conf import settings
from django.conf.urls.static import static
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Django 3.X users must add the below configuration
X_FRAME_OPTIONS = 'SAMEORIGIN'
Save the settings file and run migrations.
Now we need to add summernote to model fields, open admin.py file of the app.
from django_summernote.admin import SummernoteModelAdmin
from .models import Post
summernote_fields = ('content',)
Save the file create a superuser and navigate to the admin dashboard you should see the summernote editor on the content field.
Similarly, the editor can be applied to forms.
from django_summernote.widgets import SummernoteWidget, SummernoteInplaceWidget
# Apply summernote to specific fields.classSomeForm(forms.Form):
foo = forms.CharField(widget=SummernoteWidget()) # instead of forms.Textarea
While rendering the content in templates use the safe filter which prevents HTML from escaping.