Skip to content
Integration guide ยท Django 5 and 6 · Python 3.10+

Add a WYSIWYG editor to Django

Turn a Django form field into a rich text editor with image upload in four steps. No extra Django app to install, no API key, MIT licensed.

Step 1: Load the editor

Add the stylesheet and script to your base template from the jsDelivr CDN, or download the two files into your static folder and load them with {% static %}.

<head>
  ...
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/wds-editor@1/dist/wds-editor.min.css">
  <script src="https://cdn.jsdelivr.net/npm/wds-editor@1/dist/wds-editor.min.js"></script>
</head>

Step 2: Turn the form field into an editor

Any Textarea field works. Django gives it the id id_<field name>, and the editor keeps the textarea in sync, so the form posts as before. The file manager sends the CSRF token in the X-CSRFToken header.

{% extends "base.html" %}
{% block content %}
<form method="post">
  {% csrf_token %}
  {{ form.body }}
  <button type="submit">Save</button>
</form>

<script>
  WDSEditor.init({
    selector: '#id_body',
    preset: 'standard',
    height: 480,
    fileManager: {
      listUrl: '{% url "editor_files" %}',
      uploadUrl: '{% url "editor_upload" %}',
      headers: { 'X-CSRFToken': '{{ csrf_token }}' }
    }
  });
</script>
{% endblock %}

Step 3: Add the file manager views

Two views for signed-in users: a list of uploaded files and an upload that saves through default_storage, so MEDIA_ROOT locally and S3 or another backend in production. Add the two paths to urls.py.

import os
import secrets

from django.contrib.auth.decorators import login_required
from django.core.files.storage import default_storage
from django.http import JsonResponse
from django.utils.text import slugify
from django.views.decorators.http import require_GET, require_POST

ALLOWED = {".jpg", ".jpeg", ".png", ".gif", ".webp", ".pdf"}
MAX_SIZE = 10 * 1024 * 1024


@login_required
@require_GET
def files(request):
    names = default_storage.listdir("editor")[1] if default_storage.exists("editor") else []
    items = [
        {"url": default_storage.url(f"editor/{n}"), "name": n, "size": default_storage.size(f"editor/{n}")}
        for n in sorted(names)
        if os.path.splitext(n)[1].lower() in ALLOWED
    ]
    return JsonResponse({"files": items})


@login_required
@require_POST
def upload(request):
    f = request.FILES.get("file")
    ext = os.path.splitext(f.name)[1].lower() if f else ""
    if not f or ext not in ALLOWED or f.size > MAX_SIZE:
        return JsonResponse({"error": "Images or PDF files up to 10 MB."}, status=400)
    name = f"{slugify(os.path.splitext(f.name)[0]) or 'file'}-{secrets.token_hex(3)}{ext}"
    path = default_storage.save(f"editor/{name}", f)
    return JsonResponse({"url": default_storage.url(path), "name": os.path.basename(path)})


# urls.py
# from editor import views
# urlpatterns += [
#     path("editor/files/", views.files, name="editor_files"),
#     path("editor/upload/", views.upload, name="editor_upload"),
# ]

Step 4: Clean the HTML when you save

Clean the HTML in the form, so every view that saves it is covered. pip install nh3 installs a fast sanitizer (bleach, the older choice, is no longer developed). Show the saved HTML with {{ post.body|safe }}.

import nh3
from django import forms

from .models import Post

# Keep classes (snippet library blocks) and inline styles (colors, alignment, image width)
ATTRIBUTES = {**nh3.ALLOWED_ATTRIBUTES, "*": {"class", "style"}, "img": {"src", "alt", "width", "height"}}


class PostForm(forms.ModelForm):
    class Meta:
        model = Post
        fields = ["title", "body"]
        widgets = {"body": forms.Textarea}

    def clean_body(self):
        # data-* keeps checklists (data-type, data-checked) and mentions (data-mention, data-id)
        return nh3.clean(self.cleaned_data["body"], attributes=ATTRIBUTES, generic_attribute_prefixes={"data-"})

Good to know

  • Django admin: add the two <script>/<link> lines to a Media class on your ModelAdmin and call WDSEditor.init from a small script file.
  • In development, serve uploads with static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) in urls.py.
  • Comment forms: use preset: 'basic' and maxLength, and leave out the file manager.
  • Every option, the toolbar items and the file manager JSON format are in the documentation; the server examples add folders, rename, delete and an AI endpoint.

FAQ

Is there a pip package for WDS Editor in Django?

You do not need one. The editor is a JavaScript file and a stylesheet, and the file manager needs two small views that return JSON. That keeps you free to use your own storage, permissions and models.

Can I use WDS Editor in the Django admin?

Yes. Load the editor files through the Media class of your ModelAdmin and initialize it on the field's textarea (id_ plus the field name). The admin form posts the HTML like any other field.

How do I store uploads on S3 with Django?

Set your default storage to S3 with django-storages. The upload view uses default_storage, so files go to the bucket and the view returns their S3 URL without other changes.

Guides for other frameworks

Try WDS Editor in your browser

Free and MIT licensed, with a file manager, tables, Markdown and code blocks built in. No key, no account, no load limits.