Skip to main content

django-mirror

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.58.1) are included in the package.

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 MirrorTextarea

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

GPL. You can do what you want with this code as long as you let others do the same.

Metadata

Release files for django-mirror 0.1.6

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-mirror 0.1.6
File Size Uploaded
django-mirror-0.1.6.tar.gz 573.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-mirror 0.1.6
File Interpreter ABI Platform
django_mirror-0.1.6-py3-none-any.whl Python 3 none any Details

Total release size: 1.3 MB

Release files / django-mirror-0.1.6.tar.gz

Download URL django-mirror-0.1.6.tar.gz
Size 573.6 kB
Tags Source
SHA-256 checksum
How to use checksums
f2d6b6f7e214e0baddeed60ecb78b538b9f9f03532bf4a1dce44f2f1fad18b6d
BLAKE2b-256 checksum
How to use checksums
bc8985e47dbe5ea2dee454794c4126f9709c9ba45f8399a033b47c1a655ab1ef
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/39.0.1 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.6.9

Release files / django_mirror-0.1.6-py3-none-any.whl

Download URL django_mirror-0.1.6-py3-none-any.whl
Size 758.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
1c3694c1de31670cfcc5b30600607aaa58c719e2ada9f1074671048e1073d2c8
BLAKE2b-256 checksum
How to use checksums
f8c0ae3f120b50ec96bfd7d149936bb2c6550b5a7f70de9e10a8db6d71d72870
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/39.0.1 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.6.9

Release history Release notifications | RSS feed

0.1.11

2 release files

0.1.9

2 release files

0.1.8

2 release files

0.1.7

2 release files

This release

0.1.6 This release

2 release files

0.1.5

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.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