Skip to main content

Rich web editor widgets in the Django Admin

Project description

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-editor-widgets-4.2.tar.gz (10.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_editor_widgets-4.2-py3-none-any.whl (15.9 kB view details)

Uploaded Python 3

File details

Details for the file django-editor-widgets-4.2.tar.gz.

File metadata

  • Download URL: django-editor-widgets-4.2.tar.gz
  • Upload date:
  • Size: 10.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.8.10

File hashes

Hashes for django-editor-widgets-4.2.tar.gz
Algorithm Hash digest
SHA256 77dc3471cb1f63eba233f6a58232b7e1a196d7405c63d587307b106bf219895e
MD5 e093703801268184083a6614f358e6a2
BLAKE2b-256 d3a080ed4f1a3f8b9c8286f7d22aa224281cfd618a680180027db60a9202ba6d

See more details on using hashes here.

File details

Details for the file django_editor_widgets-4.2-py3-none-any.whl.

File metadata

File hashes

Hashes for django_editor_widgets-4.2-py3-none-any.whl
Algorithm Hash digest
SHA256 05d676a2a4eec1eebd0e3596ad5c95e159ce982d8d53e9d781b19d71777843f6
MD5 77bd99837c6af4e9a246139e5d06724a
BLAKE2b-256 2c6e16ae4d652e4b101768b1ebb0110b2c042f924518c9e86b26b1bfd9d12373

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page