Skip to main content

Custom Django field for easy use of markup in text fields

Project description

https://travis-ci.org/jamesturk/django-markupfield.svg?branch=master https://img.shields.io/pypi/v/django-markupfield.svg

An implementation of a custom MarkupField for Django. A MarkupField is in essence a TextField with an associated markup type. The field also caches its rendered value on the assumption that disk space is cheaper than CPU cycles in a web application.

Installation

The recommended way to install django-markupfield is with pip

It is not necessary to add 'markupfield' to your INSTALLED_APPS, it merely needs to be on your PYTHONPATH. However, to use titled markup you either add 'markupfield' to your INSTALLED_APPS or add the corresponding translations to your project translation.

Requirements

Requires Django >= 1.11 and Python 2.7 or 3.5+

  • 1.5.x is the last release to officially support Django < 2.0 or Python 2.7

  • 1.4.x is the last release to officially support Django < 1.11

  • 1.3.x is the last release to officially support Django 1.4 or Python 3.3

Settings

To best make use of MarkupField you should define the MARKUP_FIELD_TYPES setting, a mapping of strings to callables that ‘render’ a markup type:

import markdown
from docutils.core import publish_parts

def render_rest(markup):
    parts = publish_parts(source=markup, writer_name="html4css1")
    return parts["fragment"]

MARKUP_FIELD_TYPES = (
    ('markdown', markdown.markdown),
    ('ReST', render_rest),
)

If you do not define a MARKUP_FIELD_TYPES then one is provided with the following markup types available:

html:

allows HTML, potentially unsafe

plain:

plain text markup, calls urlize and replaces text with linebreaks

markdown:

default markdown renderer (only if python-markdown is installed)

restructuredtext:

default ReST renderer (only if docutils is installed)

It is also possible to override MARKUP_FIELD_TYPES on a per-field basis by passing the markup_choices option to a MarkupField in your model declaration.

Usage

Using MarkupField is relatively easy, it can be used in any model definition:

from django.db import models
from markupfield.fields import MarkupField

class Article(models.Model):
    title = models.CharField(max_length=100)
    slug = models.SlugField(max_length=100)
    body = MarkupField()

Article objects can then be created with any markup type defined in MARKUP_FIELD_TYPES:

Article.objects.create(title='some article', slug='some-article',
                       body='*fancy*', body_markup_type='markdown')

You will notice that a field named body_markup_type exists that you did not declare, MarkupField actually creates two extra fields here body_markup_type and _body_rendered. These fields are always named according to the name of the declared MarkupField.

Arguments

MarkupField also takes three optional arguments. Either default_markup_type and markup_type arguments may be specified but not both.

default_markup_type:

Set a markup_type that the field will default to if one is not specified. It is still possible to edit the markup type attribute and it will appear by default in ModelForms.

markup_type:

Set markup type that the field will always use, editable=False is set on the hidden field so it is not shown in ModelForms.

markup_choices:

A replacement list of markup choices to be used in lieu of MARKUP_FIELD_TYPES on a per-field basis.

escape_html:

A flag (False by default) indicating that the input should be regarded as untrusted and as such will be run through Django’s escape filter.

Examples

MarkupField that will default to using markdown but allow the user a choice:

MarkupField(default_markup_type='markdown')

MarkupField that will use ReST and not provide a choice on forms:

MarkupField(markup_type='restructuredtext')

MarkupField that will use a custom set of renderers:

CUSTOM_RENDERERS = (
    ('markdown', markdown.markdown),
    ('wiki', my_wiki_render_func)
)
MarkupField(markup_choices=CUSTOM_RENDERERS)

Accessing a MarkupField on a model

When accessing an attribute of a model that was declared as a MarkupField a special Markup object is returned. The Markup object has three parameters:

raw:

The unrendered markup.

markup_type:

The markup type.

rendered:

The rendered HTML version of raw, this attribute is read-only.

This object has a __unicode__ method that calls django.utils.safestring.mark_safe on rendered allowing MarkupField objects to appear in templates as their rendered selfs without any template tag or having to access rendered directly.

Assuming the Article model above:

>>> a = Article.objects.all()[0]
>>> a.body.raw
u'*fancy*'
>>> a.body.markup_type
u'markdown'
>>> a.body.rendered
u'<p><em>fancy</em></p>'
>>> print unicode(a.body)
<p><em>fancy</em></p>

Assignment to a.body is equivalent to assignment to a.body.raw and assignment to a.body_markup_type is equivalent to assignment to a.body.markup_type.

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-markupfield-1.5.1.tar.gz (15.9 kB view details)

Uploaded Source

Built Distribution

django_markupfield-1.5.1-py2.py3-none-any.whl (18.3 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file django-markupfield-1.5.1.tar.gz.

File metadata

  • Download URL: django-markupfield-1.5.1.tar.gz
  • Upload date:
  • Size: 15.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.21.0 setuptools/40.0.0 requests-toolbelt/0.8.0 tqdm/4.24.0 CPython/3.7.0

File hashes

Hashes for django-markupfield-1.5.1.tar.gz
Algorithm Hash digest
SHA256 c6eb2560fd5b622b57b60755f4c2884a2103f41e264cf87789323471544026a8
MD5 5a103c13502743de21878c74d12c3d60
BLAKE2b-256 8ebb4e931489838d5027e00eb78e7ee03fd6386f90b6e56202d2dd7264662361

See more details on using hashes here.

File details

Details for the file django_markupfield-1.5.1-py2.py3-none-any.whl.

File metadata

  • Download URL: django_markupfield-1.5.1-py2.py3-none-any.whl
  • Upload date:
  • Size: 18.3 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.21.0 setuptools/40.0.0 requests-toolbelt/0.8.0 tqdm/4.24.0 CPython/3.7.0

File hashes

Hashes for django_markupfield-1.5.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 f8ea8ca93aec87da51c815ac59421a0b8133ad5ad26ffabe0e0327b87d332deb
MD5 a9e36c90f3f89ccc087fd9502a394dce
BLAKE2b-256 e9d947cbfc67c73a94a905add1b294c9fa6e48b53ad44b100133f12933eb03b4

See more details on using hashes here.

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