This package marries Django with CodeMirror
Project description
This package marries Django with CodeMirror. It provides (1) a customisable form widget, and (2) a shorthand way to use this widget in the admin. In both cases the relevant static files (including mode/theme/addon files) are automatically included as form assets.
installation
This is a Python 3 package with no other dependencies apart from Django and it is offered at the Cheese Shop:
# usually inside a virtual environment
pip install django-mirror
The CodeMirror files (version 5.59.2) are included in the package. The supported Django versions are 2.2, 3.0, and 3.1.
settings
# add 'django_mirror' to your INSTALLED_APPS if you want the package to be
# handled by Django's collectstatic command
INSTALLED_APPS += ['django_mirror']
# use DJANGO_MIRROR_DEFAULTS to specify default options for your widgets
# see the next section for more info about the options
DJANGO_MIRROR_DEFAULTS = {
'mode': 'rst',
'addons': ['mode/overlay'],
'line_wrapping': True,
}
widget
Bascially this package provides a form widget called MirrorArea that extends Django’s Textarea widget.
from django import forms
from django_mirror.widgets import MirrorArea
class CommentForm(forms.Form):
text = forms.CharField(
widget=MirrorArea(
attrs={'rows': 20}, # the parent class' attrs still works
mode='markdown', # the other kwargs are forwarded to CodeMirror
)
)
The MirrorArea widget can be initialised with the following arguments:
Any of CodeMirror’s config options. These can be specified in either camelCase or snake_case (e.g. both tabSize and tab_size would work). The css/js files associated with the mode, if provided, are included as form assets.
addons, a list of CodeMirror addons, e.g. dialog/dialog. The css/js files associated with the addons are recursively included as form assets.
attrs, just as Django’s form widgets.
The addons and config options are merged with and override DJANGO_MIRROR_DEFAULTS if the setting has been defined.
admin
If you want to use the widget in the admin panel, you can subclass the MirrorAdmin mixin, which provides the mirror_fields model admin option:
from django.contrib import admin
from django_mirror.admin import MirrorAdmin
from weblog.models import Comment
@admin.register(Comment)
class CommentAdmin(MirrorAdmin, admin.ModelAdmin):
mirror_fields = ('comment',) # default options
mirror_fields = ( # with custom options
('comment', {
'mode': 'markdown',
'line_wrapping': True,
}),
)
The mixin also includes a bit of css to make CodeMirror look more like regular admin textarea fields.
similar projects
There are several other packages that provide customisable CodeMirror widgets:
licence
GNU LGPLv3. Please refer to COPYING for the base GPLv3 licence and to COPYING.LESSER for the additional permissions on top that constitute LGPLv3.
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
File details
Details for the file django-mirror-0.1.11.tar.gz
.
File metadata
- Download URL: django-mirror-0.1.11.tar.gz
- Upload date:
- Size: 588.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/44.0.0 requests-toolbelt/0.9.1 tqdm/4.56.1 CPython/3.8.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | be3eb35e19ada63ecae799d10c8598d73dd9c56a1c5195622961ebcde62f7887 |
|
MD5 | 5bc5eb55ba467d15c4dc89f10a919c6e |
|
BLAKE2b-256 | 6d6906cccf9c669216b9cc5467533472a46c009b40cd62b8d602b4a3b2a8cc5c |
Provenance
File details
Details for the file django_mirror-0.1.11-py3-none-any.whl
.
File metadata
- Download URL: django_mirror-0.1.11-py3-none-any.whl
- Upload date:
- Size: 766.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/44.0.0 requests-toolbelt/0.9.1 tqdm/4.56.1 CPython/3.8.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 667accb86972a02f87073d2d338c548a82453bbe4e74ebf822914ba3be7abc7b |
|
MD5 | 564480c87ba761943c98f2f8b229bcd2 |
|
BLAKE2b-256 | 9afaf6760bb20ac1d42b37e3c5811730d2ba54dfbb011a44b181ed9e3957f8d2 |