Skip to main content

Django-editor-widgets

This package provides some custom widgets to use monaco or tinymce editors in django admin.

remark: From version 4.0 static files for tinymce and monaco editors are removed.

Installation

To install the package by pip run following command

# From Github (latest updates)
$ pip install git+https://github.com/giorgi94/django-editor-widgets.git
# Or
$ pip install django-editor-widgets

Usage

To start using the package in your project, you need to open settings.py file and add following lines

# settings.py
from pathlib import Path

# import configurations for editor
from djangoeditorwidgets.config import init_web_editor_config

# set base dir by Path instead of os.path
BASE_DIR = Path(__file__).resolve().parent.parent

# Application definition

INSTALLED_APPS = [
    ...
    'djangoeditorwidgets',
    ...
]


MEDIA_URL = "/media/"
MEDIA_ROOT = BASE_DIR / "media"

STATIC_URL = "/static/"
STATIC_ROOT = BASE_DIR / "static"

WEB_EDITOR_DOWNLOAD, WEB_EDITOR_CONFIG = init_web_editor_config(
    # set the directory where files are downloaded
    BASE_DIR / "static_cdn",
    # set static url prefix
    STATIC_URL
)

In current version uses Path instead of os.path to manage files and directories. If needed configuration parameters can be written manually

from os.path import join

WEB_EDITOR_DOWNLOAD = {
    "to": BASE_DIR / "static_cdn",
    "tinymce": {
        "url": "https://download.tiny.cloud/tinymce/community/tinymce_5.10.3.zip",
        "target": "tinymce/js/tinymce",
    },
    "monaco": {
        "url": "https://registry.npmjs.org/monaco-editor/-/monaco-editor-0.32.1.tgz",
        "target": "package/min",
    },
}

WEB_EDITOR_CONFIG = {
    "tinymce": {
        "js": [
            join(STATIC_URL, "tinymce/tinymce.min.js"),
            join(STATIC_URL, "djangoeditorwidgets/tinymce/tinymce.config.js"),
            join(STATIC_URL, "djangoeditorwidgets/tinymce/tinymce.init.js"),
        ],
        "css": {
            "all": [
                join(STATIC_URL, "djangoeditorwidgets/tinymce/tinymce.custom.css"),
            ]
        },
    },
    "monaco": {
        "js": [
            join(STATIC_URL, "monaco/vs/loader.js"),
            join(STATIC_URL, "djangoeditorwidgets/monaco/monaco.config.js"),
        ],
        "css": {
            "all": [
                join(STATIC_URL, "djangoeditorwidgets/monaco/monaco.custom.css"),
            ]
        },
    },
}

Commands

Static files are removed from the package, instead it provides management command to download and extract files to given location

$ python manage.py download_editpr_scripts

TinyMCE

To use tinymce editor in admin, we need to change default widget in the form with TinymceWidget

# forms.py
from django import forms
from djangoeditorwidgets.widgets import TinymceWidget
from .models import TextModel


class TextModelForm(forms.ModelForm):

    class Meta:
        model = TextModel
        fields = '__all__'
        widgets = {
            'text': TinymceWidget(name="default")
        }

By name argument we can modify selector and can define multiple configs for tinymce editor.

Monaco Editor

In lastest verions of sqlite3 and mariadb we now have json field, it is more limited compared to postgresql but can handle json validation and parsing

# models.py
import json
from django.db import models

class JSONModel(models.Model):
    title = models.CharField(max_length=50)
    text = models.JSONField(null=True)

    def __str__(self):
        return self.title


# forms.py
from django import forms
from djangoeditorwidgets.widgets import MonacoEditorWidget
from .models import JSONModel


class JsonModelForm(forms.ModelForm):
    class Meta:
        model = JSONModel
        fields = "__all__"
        widgets = {
            "text": MonacoEditorWidget(name="default", language="json", wordwrap=True)
        }

Remark

Unlike other django package which are for Rich web editors, this package allows developer to fully use capablities and custom plugins for the web editor, like tinymce or monaco, since configurations doesn't go through django settings.

Metadata

Release files for django-editor-widgets 4.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-editor-widgets 4.2
File Size Uploaded
django-editor-widgets-4.2.tar.gz 10.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-editor-widgets 4.2
File Interpreter ABI Platform
django_editor_widgets-4.2-py3-none-any.whl Python 3 none any Details

Total release size: 26.2 kB

Release files / django-editor-widgets-4.2.tar.gz

Download URL django-editor-widgets-4.2.tar.gz
Size 10.2 kB
Tags Source
SHA-256 checksum
How to use checksums
77dc3471cb1f63eba233f6a58232b7e1a196d7405c63d587307b106bf219895e
BLAKE2b-256 checksum
How to use checksums
d3a080ed4f1a3f8b9c8286f7d22aa224281cfd618a680180027db60a9202ba6d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.8.10

Release files / django_editor_widgets-4.2-py3-none-any.whl

Download URL django_editor_widgets-4.2-py3-none-any.whl
Size 15.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
05d676a2a4eec1eebd0e3596ad5c95e159ce982d8d53e9d781b19d71777843f6
BLAKE2b-256 checksum
How to use checksums
2c6e16ae4d652e4b101768b1ebb0110b2c042f924518c9e86b26b1bfd9d12373
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.8.10

Release history Release notifications | RSS feed

This release

4.2 This release

2 release files

4.1

2 release files

4.0

2 release files

3.0

2 release files

2.0

2 release files

1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page