Package: django-markitup-field
An implementation of a custom MarkupField for Django.  A MarkupField is in
essence a TextField with an associated markup format andMarkItUp!markup
editor widget with AJAX preview.  The field also caches its rendered value on
the assumption that disk space is cheaper than CPU cycles in a web application.Based ondjango-markupfieldanddjango-markitup.InstallationThe recommended way to install django-markitup-field is withpipInstall from PyPI witheasy_installorpip:pip install django-markitup-fieldor get thein-development version:pip install django-markitup-field==tipIt is not necessary to add'markitup_field'to yourINSTALLED_APPS, it
merely needs to be on yourPYTHONPATH.If you want to use AJAX-based preview, addurl(r'^markitup/',include('markitup.urls'))in your root URLconf.Requirementsdjango-markitup-field depends on a relatively current version of Django
(tested with 1.3-1.4, may work with 1.2 but not guaranteed) and libraries for
whichever markup options you wish to include.SettingsYou can define theMARKUP_FILTERSsetting, 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_FILTERS = (
    ('markdown', markdown.markdown),
    ('restructuredtext', render_rest),
)If you do not define aMARKUP_FILTERSthen one is provided with the
following markup types available:html:allows HTML, potentially unsafetext:plain text markup, calls urlize and replaces text with linebreaksmarkdown:defaultmarkdownrenderer (only ifpython-markdownis installed)restructuredtext:defaultReSTrenderer (only ifdocutilsis installed)textile:defaulttextilerenderer (only iftextileis installed)UsageUsing MarkupField is relatively easy, it can be used in any model definition:from django.db import models
from markitup_field.fields import MarkupField

class Article(models.Model):
    title = models.CharField(max_length=100)
    slug = models.SlugField(max_length=100)
    body = MarkupField()Articleobjects can then be created with any markup type defined inMARKUP_FORMATS:Article.objects.create(title='some article', slug='some-article',
                       body='*fancy*', body_markup_format='markdown')You will notice that a field namedbody_markup_formatexists that you did
not declare, MarkupField actually creates two extra fields.body_markup_formatThis field is always named according to the name of the declaredMarkupField.ArgumentsMarkupFieldalso takes three optional arguments.  Eitherdefault_markup_formatandmarkup_formatarguments may be specified but
not both.default_markup_format: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_format:Set markup type that the field will always use,editable=Falseis 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 ofMARKUP_FORMATSon 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’sescapefilter.rendered_field_name:Name for field with rendered content. If it is set to None,
then it named <field_name>_renderedExamplesMarkupFieldthat will default to using markdown but allow the user a choice:MarkupField(default_markup_type='markdown')MarkupFieldthat will use textile and not provide a choice on forms:MarkupField(markup_type='textile')MarkupFieldthat 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 modelWhen accessing an attribute of a model that was declared as aMarkupFielda specialMarkupobject is returned.  TheMarkupobject has three
parameters:raw:The unrendered markup.markup_format:The markup type.rendered:The rendered HTML version ofraw, this attribute is read-only.This object has a__unicode__method that callsdjango.utils.safestring.mark_safeonrenderedallowing MarkupField
objects to appear in templates as their rendered selfs without any template
tag or having to accessrendereddirectly.Assuming theArticlemodel 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 toa.bodyis equivalent to assignment toa.body.rawand
assignment toa.body_markup_formatis equivalent to assignment toa.body.markup_format.Notea.body.rendered is only updated when a.save() is called