Django TinyMCE

5.0.0 · active · verified Mon Apr 13

django-tinymce is a Django application that provides a widget to easily integrate the TinyMCE rich text editor into Django forms and models. It allows for highly customizable WYSIWYG editing experiences within Django projects, including the admin interface. The library is actively maintained with regular updates to support newer TinyMCE and Django versions.

Warnings

Install

Imports

Quickstart

To quickly set up django-tinymce, first install the package and add 'tinymce' to your `INSTALLED_APPS`. Include `tinymce.urls` in your project's `urls.py`. Then, configure TinyMCE settings in `settings.py`, paying attention to `TINYMCE_JS_URL` for the TinyMCE JavaScript source (either self-hosted or CDN) and `TINYMCE_DEFAULT_CONFIG` for editor options. You can use `tinymce.models.HTMLField` directly in your models or `tinymce.widgets.TinyMCE` in your Django forms or admin definitions to apply the rich text editor.

# settings.py
import os

INSTALLED_APPS = [
    # ...
    'tinymce',
]

# Configure TinyMCE (adjust TINYMCE_JS_URL based on self-hosted or CDN)
TINYMCE_JS_URL = 'https://cdn.tiny.cloud/1/no-api-key/tinymce/6/tinymce.min.js' # Example using CDN with no API key
# OR for self-hosted: TINYMCE_JS_URL = os.path.join(STATIC_URL, 'tinymce/tinymce.min.js')

TINYMCE_DEFAULT_CONFIG = {
    'height': 360,
    'menubar': False,
    'plugins': 'advlist autolink lists link image charmap print preview anchor searchreplace visualblocks code fullscreen insertdatetime media table paste code help wordcount',
    'toolbar': 'undo redo | formatselect | bold italic backcolor | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | removeformat | help',
    'custom_undo_redo_levels': 10,
    'browser_spellcheck': True, # Important for TinyMCE 6+ spellcheck
}

# urls.py (project level)
from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('tinymce/', include('tinymce.urls')), # Required for TinyMCE views
]

# models.py (your app)
from django.db import models
from tinymce.models import HTMLField

class MyRichTextModel(models.Model):
    title = models.CharField(max_length=200)
    content = HTMLField()

    def __str__(self):
        return self.title

# admin.py (your app)
from django.contrib import admin
from .models import MyRichTextModel
from tinymce.widgets import TinyMCE
from django import forms

class MyRichTextAdminForm(forms.ModelForm):
    content = forms.CharField(widget=TinyMCE(attrs={'cols': 80, 'rows': 30}))

    class Meta:
        model = MyRichTextModel
        fields = '__all__'

@admin.register(MyRichTextModel)
class MyRichTextModelAdmin(admin.ModelAdmin):
    form = MyRichTextAdminForm

view raw JSON →