Skip to content

Instantly share code, notes, and snippets.

@caramdache
Last active August 23, 2022 07:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save caramdache/0720525459b0edaea97ff9f1df7734dd to your computer and use it in GitHub Desktop.
Save caramdache/0720525459b0edaea97ff9f1df7734dd to your computer and use it in GitHub Desktop.
Django efficient multi-editors using tinymce

admin.py

from django.contrib import admin
from django.contrib.admin import widgets

@admin.register(MyModel)
class MyModelAdmin(admin.ModelAdmin):
    change_form_template = "tinymce/change_form.html"
    change_list_template = "tinymce/change_list.html"

    def formfield_for_dbfield(self, db_field, **kwargs):
        if db_field.name == 'comments':
            kwargs['widget'] = widgets.AdminTextareaWidget()

tinymce/change_form.html

{% extends 'admin/change_form.html' %}

{% block extrahead %}
{{ block.super }}
{% include "tinymce/include/head.html" %}
{% endblock %}

{% block content %}
{{ block.super }}
{% include "tinymce/include/script.html" %}
{% endblock %}

tinymce/change_list.html

{% extends 'admin/change_list.html' %}

{% block extrahead %}
{{ block.super }}
{% include "tinymce/include/head.html" %}
{% endblock %}

{% block content %}
{{ block.super }}
{% include "tinymce/include/script.html" %}
{% endblock %}

tinymce/include/head.html

<script src="https://cdn.tiny.cloud/1/no-api-key/tinymce/6/tinymce.min.js" referrerpolicy="origin"></script>

tinymce/include/script.html

<script>
tinymce.init({
	selector: 'textarea.vLargeTextField', <!-- One single editor for all textareas -->
	width: 600,
	height: 100,
	max_height: 300,
	autoresize_bottom_margin: 0,
	resize: true,
	menubar: false,
	toolbar: false,
	statusbar: false,
	branding: false,
	plugins: 'autoresize quickbars lists  table autolink help',

	quickbars_insert_toolbar: false,
	quickbars_selection_toolbar: 'bold italic underline strikethrough  | bullist numlist  | blockquote',
	contextmenu: 'undo redo | inserttable | cell row column deletetable | help',
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment