Custom Django field for easy use of markup in text fields
An implementation of a custom MarkupField for Django. A MarkupField is in essence a TextField with an associated markup format and MarkItUp! 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.
The recommended way to install django-markitup-field is with pip
Install from PyPI with easy_install or pip:
pip install django-markitup-field
or get the in-development version:
pip install django-markitup-field==tip
It is not necessary to add 'markitup_field' to your INSTALLED_APPS, it merely needs to be on your PYTHONPATH.
If you want to use AJAX-based preview, add url(r'^markitup/', include('markitup.urls')) in your root URLconf.
django-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.
You can define the MARKUP_FILTERS 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_FILTERS = ( ('markdown', markdown.markdown), ('restructuredtext', render_rest), )
If you do not define a MARKUP_FILTERS then one is provided with the following markup types available:
Using 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()
Article objects can then be created with any markup type defined in MARKUP_FORMATS:
Article.objects.create(title='some article', slug='some-article', body='*fancy*', body_markup_format='markdown')
You will notice that a field named body_markup_format exists that you did not declare, MarkupField actually creates two extra fields. body_markup_format This field is always named according to the name of the declared MarkupField.
MarkupField also takes three optional arguments. Either default_markup_format and markup_format arguments may be specified but not both.
MarkupField that will default to using markdown but allow the user a choice:
MarkupField that will use textile and not provide a choice on forms:
MarkupField that will use a custom set of renderers:
CUSTOM_RENDERERS = ( ('markdown', markdown.markdown), ('wiki', my_wiki_render_func) ) MarkupField(markup_choices=CUSTOM_RENDERERS)
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:
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() >>> 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_format is equivalent to assignment to a.body.markup_format.
a.body.rendered is only updated when a.save() is called