Skip to main content

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.58.2) 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

GNU GPLv3.

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-mirror-0.1.7.tar.gz (573.9 kB view details)

Uploaded Source

Built Distribution

django_mirror-0.1.7-py3-none-any.whl (758.8 kB view details)

Uploaded Python 3

File details

Details for the file django-mirror-0.1.7.tar.gz.

File metadata

  • Download URL: django-mirror-0.1.7.tar.gz
  • Upload date:
  • Size: 573.9 kB
  • Tags: Source
  • Uploaded using 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

File hashes

Hashes for django-mirror-0.1.7.tar.gz
Algorithm Hash digest
SHA256 a8c8261b8426f17de62aa4d724b8449d69f3fae8195d89af34cf84fb26053d7a
MD5 9ec2bd8be975f8903bac490e03413474
BLAKE2b-256 0c93bebe4e0d207b5d5e7ba4123e2ff739f8a6893d51388ae8b85290853532eb

See more details on using hashes here.

Provenance

File details

Details for the file django_mirror-0.1.7-py3-none-any.whl.

File metadata

  • Download URL: django_mirror-0.1.7-py3-none-any.whl
  • Upload date:
  • Size: 758.8 kB
  • Tags: Python 3
  • Uploaded using 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

File hashes

Hashes for django_mirror-0.1.7-py3-none-any.whl
Algorithm Hash digest
SHA256 d5e9c49079a3f4c1845e27d7aac16b9e0dc34001d546d9adc758edd145675988
MD5 9f15c1ab4930128666d6b0a77a32d366
BLAKE2b-256 8c30b74ad0412c62ffaba24c4112baf73790f09642b6b38ae1812535b3a258cf

See more details on using hashes here.

Provenance

Supported by

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