A Django admin mixin to link MonacoEditor CSS and TinyMCE HTML fields for a live preview.
Project description
Django Admin Style Linker
A simple Django admin mixin to link a CSS editor field (MonacoEditor) with an HTML editor field (TinyMCE) for a live preview. When a user types CSS, it's instantly applied to the content in the HTML editor's iframe.
Features
- Easy to integrate with any
ModelAdmin. - Works with separate CSS and HTML fields.
- Optional support for django-modeltranslation.
Installation
pip install django-admin-style-linker
Add admin_style_linker to your INSTALLED_APPS in settings.py and configure TinyMCE:
INSTALLED_APPS = [
# ...
'django_monaco_editor',
'tinymce',
'admin_style_linker',
# ...
]
# Important
# You need to add this setup to your TinyMCE settings for django-admin-style-linker to work properly.
TINYMCE_DEFAULT_CONFIG = {
"setup": """function (editor) {
const event = new CustomEvent('tinyMceAllEditorsInit', { detail: [editor] });
document.dispatchEvent(event);
}""",
# other TinyMCE settings
}
Usage
In your admin.py, inherit from LinkStyleAdminMixin and configure the link_styles attribute.
link_styles is a list of dictionaries, where each dictionary specifies one style field and one or more HTML fields it should apply to.
Example:
from django.contrib import admin
from .models import MyModel
from admin_style_linker import LinkStyleAdminMixin
@admin.register(MyModel)
class MyModelAdmin(LinkStyleAdminMixin, admin.ModelAdmin):
# ... other admin settings
link_styles = [
{
'styles_field': 'custom_css', # The name of the field with CSS
'html_fields': ['content_html', 'another_html_field'], # A list of fields with HTML
},
]
Django Modeltranslation Support
The mixin automatically detects if django-modeltranslation is installed and intelligently handles translated fields. You don't need any special configuration—just use the base field names in your link_styles setup, and the mixin will figure out the rest.
Here are a few common scenarios:
Scenario 1: Both Style and HTML Fields are Translatable (Most common)
When both the CSS field and the HTML field(s) are registered for translation, the mixin will link fields of the same language together.
translation.py:
from modeltranslation.translator import register, TranslationOptions
from .models import MyModel
@register(MyModel)
class MyModelTranslationOptions(TranslationOptions):
fields = ('styles', 'content')
admin.py:
@admin.register(MyModel)
class MyModelAdmin(LinkStyleAdminMixin, admin.ModelAdmin):
link_styles = [
{
'styles_field': 'styles', # Base name
'html_fields': ['content'], # Base name
},
]
Result: The mixin will automatically create the following links:
styles_enwill apply tocontent_en.styles_ukwill apply tocontent_uk.- ...and so on for all your
LANGUAGES.
Scenario 2: Non-Translatable Styles for Translatable HTML
Use one common CSS field to style all language versions of your HTML content.
translation.py:
@register(MyModel)
class MyModelTranslationOptions(TranslationOptions):
fields = ('content',) # Only 'content' is translatable
admin.py:
@admin.register(MyModel)
class MyModelAdmin(LinkStyleAdminMixin, admin.ModelAdmin):
link_styles = [
{
'styles_field': 'global_styles', # Not a translatable field
'html_fields': ['content'], # Translatable field
},
]
Result:
The global_styles field will apply to all language versions of the content field:
global_styles→content_englobal_styles→content_uk
Scenario 3: Translatable Styles for a Non-Translatable HTML Field
If your style field is translatable but the HTML field is not, the mixin will link the non-translatable field only to the style field of the default language (settings.LANGUAGE_CODE).
translation.py:
@register(MyModel)
class MyModelTranslationOptions(TranslationOptions):
fields = ('styles',) # Only 'styles' is translatable
admin.py:
@admin.register(MyModel)
class MyModelAdmin(LinkStyleAdminMixin, admin.ModelAdmin):
link_styles = [
{
'styles_field': 'styles', # Translatable field
'html_fields': ['footer_html'], # NOT a translatable field
},
]
Result:
Assuming your default language is English (en):
styles_enwill apply tofooter_html.- Other style versions (
styles_uk, etc.) will not be linked tofooter_html.
How it Works
The mixin injects data-* attributes into the widgets of the specified fields during form rendering. A JavaScript file then listens for changes in the data-style-source-for field and injects its content as a <style> tag into the <iframe> of the corresponding data-style-target-of editor.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file django_admin_style_linker-1.0.3.tar.gz.
File metadata
- Download URL: django_admin_style_linker-1.0.3.tar.gz
- Upload date:
- Size: 10.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6e6e47453846823ed847657fe22854bebc871e60b29da70ce9c5d35cc8bcab79
|
|
| MD5 |
1e25b0b3cd20afcfef93b188a5747c02
|
|
| BLAKE2b-256 |
e3480a2e395dd6600f7196f0177aebc88ac7e683b4d91b2f31b50ba6fc503a42
|
Provenance
The following attestation bundles were made for django_admin_style_linker-1.0.3.tar.gz:
Publisher:
release.yml on john-psina/django-admin-style-linker
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
django_admin_style_linker-1.0.3.tar.gz -
Subject digest:
6e6e47453846823ed847657fe22854bebc871e60b29da70ce9c5d35cc8bcab79 - Sigstore transparency entry: 655407086
- Sigstore integration time:
-
Permalink:
john-psina/django-admin-style-linker@0aa9b3817c9e7babffe560f29b50823b4b1cd75e -
Branch / Tag:
refs/heads/main - Owner: https://github.com/john-psina
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@0aa9b3817c9e7babffe560f29b50823b4b1cd75e -
Trigger Event:
push
-
Statement type:
File details
Details for the file django_admin_style_linker-1.0.3-py3-none-any.whl.
File metadata
- Download URL: django_admin_style_linker-1.0.3-py3-none-any.whl
- Upload date:
- Size: 9.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d92e34bdfd227b9be20872ef9b7eb9c2db35f3e93fdce1e805215c7f63fb24ed
|
|
| MD5 |
65a6718457131f085d20a9bcc8af5ab7
|
|
| BLAKE2b-256 |
3dfeb8e8aa70e85684dc96f00e72cc08705e8bd1078f29f5e1d972bcbd40d9a4
|
Provenance
The following attestation bundles were made for django_admin_style_linker-1.0.3-py3-none-any.whl:
Publisher:
release.yml on john-psina/django-admin-style-linker
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
django_admin_style_linker-1.0.3-py3-none-any.whl -
Subject digest:
d92e34bdfd227b9be20872ef9b7eb9c2db35f3e93fdce1e805215c7f63fb24ed - Sigstore transparency entry: 655407136
- Sigstore integration time:
-
Permalink:
john-psina/django-admin-style-linker@0aa9b3817c9e7babffe560f29b50823b4b1cd75e -
Branch / Tag:
refs/heads/main - Owner: https://github.com/john-psina
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@0aa9b3817c9e7babffe560f29b50823b4b1cd75e -
Trigger Event:
push
-
Statement type: