Package: django-markitup
A Django reusable application for end-to-end markup handling. Includes:Easy integration of theMarkItUp!markup editor widget (by Jay
Salvat) in Django projects, with server-side support for MarkItUp!’s
AJAX preview. Plug in MarkItUp! via form widget or template tags.MarkupField, aTextFieldthat automatically renders and
stores both its raw and rendered values in the database, on the
assumption that disk space is cheaper than CPU cycles in a web
application.InstallationInstall from PyPI:pip install django-markitupTo usedjango-markitupin your Django project:Add'markitup'to yourINSTALLED_APPSsetting.Make the contents of themarkitup/static/markitupdirectory
available atSTATIC_URL/markitup; the simplest way is viadjango.contrib.staticfiles.Setthe MARKITUP_FILTER setting.If you want to use AJAX-based preview, addurl(r'^markitup/',include('markitup.urls'))in your root URLconf.Dependenciesdjango-markitup4.1.x requiresDjango2.2 or later and Python 3.5+.django-markitup4.0.x requiresDjango1.11 or later and Python 3.5+.django-markitup3.x requiresDjango1.8 or later and Python 2.7+ or 3.4+.django-markitup2.x requiresDjango1.4 or later and Python 2.6+ or 3.3+.django-markitup1.x requiresDjango1.3 or later and Python 2.5 or later.MarkItUp!is not an external dependency; it is bundled withdjango-markitup.Using the MarkItUp! widgetThe MarkItUp! widget lives atmarkitup.widgets.MarkItUpWidget, and
can be used like any other Django custom widget.To assign it to a form field:from markitup.widgets import MarkItUpWidget

class MyForm(forms.Form):
    content = forms.CharField(widget=MarkItUpWidget())When this form is displayed on your site, you must include the form media
somewhere on the page using{{ form.media }}, or the MarkItUpWidget will
have no effect. By default{{ form.media }}also includes the jQuery
library based on yourJQUERY_URLsetting. To prevent including jQuery, set
theJQUERY_URLsetting toNone.MarkItUpWidget accepts three optional keyword arguments:markitup_setandmarkitup_skin(seeChoosing a MarkItUp!
button set and skin) andauto_preview(to override the value of
theMARKITUP_AUTO_PREVIEWsetting).To use the widget in the Django admin:from markitup.widgets import AdminMarkItUpWidget

class MyModelForm(forms.ModelForm):
    content = forms.CharField(widget=AdminMarkItUpWidget())
    # ...

class MyModelAdmin(admin.ModelAdmin):
    form = MyModelForm
    # ...You can also use the formfield_overrides attribute of the ModelAdmin, which
is simpler but only allows setting the widget per field type (so it isn’t
possible to use the MarkItUpWidget on one TextField in a model and not
another):from markitup.widgets import AdminMarkItUpWidget

class MyModelAdmin(admin.ModelAdmin):
    formfield_overrides = {models.TextField: {'widget': AdminMarkItUpWidget}}If you useMarkupFieldin your model, it is rendered in the adminwith anAdminMarkItUpWidgetby default.Using MarkItUp! via templatetagsIn some cases it may be inconvenient to useMarkItUpWidget(for
instance, if the form in question is defined in third-party code). For
these cases, django-markitup provides template tags to achieve the
same effect purely in templates.First, load the django-markitup template tag library:{% load markitup_tags %}Then include the MarkItUp! CSS and Javascript in the <head> of your page:{% markitup_media %}By default themarkitup_mediatag also includes jQuery, based on the value
of yourJQUERY_URLsetting, with a fallback to the version hosted at Google
Ajax APIs. To suppress the inclusion of jQuery (if you are already including it
yourself), set theJQUERY_URLsetting toNone.If you prefer to link CSS and Javascript from different locations, themarkitup_mediatag can be replaced with two separate tags,markitup_cssandmarkitup_js.markitup_jsaccepts a
parameter to suppress jQuery inclusion, just likemarkitup_media.Last, use themarkitup_editortemplate tag to apply the MarkItUp!
editor to a textarea in your page. It accepts one argument, the HTML
id of the textarea. If you are rendering the textarea in the usual way
via a Django form object, that id value is available asform.fieldname.auto_id:{{ form.fieldname }}

{% markitup_editor form.fieldname.auto_id %}You can usemarkitup_editoron as many different textareas as you
like.markitup_editoraccepts an optional second parameter, which can be
either"auto_preview"or"no_auto_preview"to override the
value of theMARKITUP_AUTO_PREVIEWsetting.The actual HTML included by these templatetags is defined by the
contents of the templatesmarkitup/include_css.html,markitup/include_js.html, andmarkitup/editor.html. You can
override these templates in your project and customize them however
you wish.MarkupFieldYou can apply the MarkItUp! editor control to any textarea using the
above techniques, and handle the markup on the server side however you
prefer.For a seamless markup-handling solution, django-markitup also provides
aMarkupFieldmodel field that automatically renders and stores
both its raw and rendered values in the database, using the value ofthe MARKITUP_FILTER settingto parse the markup into HTML.AMarkupFieldis easy to add to any model definition:from django.db import models
from markitup.fields import MarkupField

class Article(models.Model):
    title = models.CharField(max_length=100)
    body = MarkupField()MarkupFieldautomatically creates an extra non-editable field_body_renderedto store the rendered markup. This field doesn’t
need to be accessed directly; see below.Accessing a MarkupField on a modelWhen accessing an attribute of a model that was declared as aMarkupField, aMarkupobject is returned.  TheMarkupobject has two attributes:raw:The unrendered markup.rendered:The rendered HTML version ofraw(read-only).This object also has a__unicode__method that callsdjango.utils.safestring.mark_safeonrendered, allowingMarkupFieldattributes to appear in templates as rendered HTML
without any special template tag or having to accessrendereddirectly.Assuming theArticlemodel above:>>> a = Article.objects.all()[0]
>>> a.body.raw
'*fancy*'
>>> a.body.rendered
'<p><em>fancy</em></p>'
>>> print(unicode(a.body))
<p><em>fancy</em></p>
>>> a.body.render_with('markitup.renderers.render_rest')
>>> print(unicode(a.body))
'<div class="document">\n<p><em>fancy</em></p>\n</div>\n'Assignment toa.bodyis equivalent to assignment toa.body.raw.Notea.body.rendered is only updated when a.save() or a.body.render_with()
is calledEditing a MarkupField in a formWhen editing aMarkupFieldmodel attribute in aModelForm(i.e. in the Django admin), you’ll generally want to edit the original
markup and not the rendered HTML.  Because theMarkupobject
returns rendered HTML from its __unicode__ method, it’s necessary to
use theMarkupTextareawidget from themarkupfield.widgetsmodule, which knows to return the raw markup instead.By default, aMarkupFielduses the MarkItUp! editor control in the
admin (via the providedAdminMarkItUpWidget), but a plainMarkupTextareain other forms. If you wish to use the MarkItUp!
editor with thisMarkupFieldin your own form, you’ll need to use
the providedMarkItUpWidgetrather thanMarkupTextarea.If you apply your own custom widget to the form field representing aMarkupField, your widget must either inherit fromMarkupTextareaor itsrendermethod must convert itsvalueargument tovalue.raw.Choosing a MarkItUp! button set and skinMarkItUp! allows the toolbar button-set to be customized in a
Javascript settings file.  By default, django-markitup uses the
“default” set (meant for HTML editing).  Django-markitup also includes
basic “markdown” and “textile” sets (these are available fromthe
MarkItUp site), as well as a
“restructuredtext” set.To use an alternate set, assign theMARKITUP_SETsetting a URL path
(absolute or relative toSTATIC_URL) to the set directory.  For
instance, to use the “markdown” set included with django-markitup:MARKITUP_SET = 'markitup/sets/markdown/'MarkItUp! skins can be specified in a similar manner.  Both “simple”
and “markitup” skins are included, by default “simple” is used.  To
use the “markitup” skin instead:MARKITUP_SKIN = 'markitup/skins/markitup/'Neither of these settings has to refer to a location inside
django-markitup’s media.  You can define your own sets and skins and
store them anywhere, as long as you set the MARKITUP_SET and
MARKITUP_SKIN settings to the appropriate URLs.Set and skin may also be chosen on a per-widget basis by passing themarkitup_setandmarkitup_skinkeyword arguments to
MarkItUpWidget.Using AJAX previewIf you’ve includedmarkitup.urlsin your root URLconf (as
demonstrated above underInstallation), all you need to enable
server-side AJAX preview isthe MARKITUP_FILTER setting.The rendered HTML content is displayed in the Ajax preview wrapped by
an HTML page generated by themarkitup/preview.htmltemplate; you
can override this template in your project and customize the preview
output.NoteUsing the MarkItUpWidget ormarkitup_editortemplate tag will
automatically set thepreviewParserPathin your MarkItUp! set
toreverse('markitup_preview'), ifmarkitup.urlsis
included in your URLconf.The MARKITUP_FILTER settingTheMARKITUP_FILTERsetting defines how markup is transformed into
HTML on your site. This setting is only required if you are usingMarkupFieldor MarkItUp! AJAX preview.MARKITUP_FILTERmust be a two-tuple. The first element must be a
string, the Python dotted path to a markup filter function.  This
function should accept markup as its first argument and return HTML.
It may accept other keyword arguments as well.  You may parse your
markup using any method you choose, as long as you can wrap it in a
function that meets these criteria.The second element must be a dictionary of keyword arguments to pass
to the filter function.  The dictionary may be empty.For example, if you have python-markdown installed, you could use it
like this:MARKITUP_FILTER = ('markdown.markdown', {})Alternatively, you could use the “textile” filter provided by the
django-markwhat library like this:MARKITUP_FILTER = ('django_markwhat.templatetags.markup.textile', {})(The textile filter function doesn’t accept keyword arguments, so the
kwargs dictionary must be empty in this case.)django-markitupprovides one sample rendering function,render_restin themarkitup.renderersmodule.Avoiding Cross Site Scripting (XSS) attacksIf your site is displaying user-provided markup to the world, then there
is some risk of users injecting malicious<script>tags (or similar)
into their markup text, that your site will then render in its HTML
pages. Any filter that passes through HTML unmodified (e.g.
python-markdown) is especially at-risk, here.This can be mitigated by running the rendered HTML through the bleach
library, e.g:MARKITUP_FILTER = ('my_lib.bleached_markdown', {})Wheremy_libcontains:import bleach
from bleach_allowlist import markdown_tags, markdown_attrs
from markdown import markdown

def bleached_markdown(text, **kwargs):
    markdown_rendered = markdown(text, **kwargs)
    bleached = bleach.clean(markdown_rendered, markdown_tags, markdown_attrs)
    return bleachedrender_markup template filterIf you have setthe MARKITUP_FILTER settingand use the MarkItUp!
AJAX preview, but don’t wish to store rendered markup in the database
withMarkupField(or are using third-party models that don’t useMarkupField), you may want a convenient way to render content in
your templates using your MARKITUP_FILTER function. For this you can
use therender_markuptemplate filter:{% load markitup_tags %}

{{ post.content|render_markup }}Other settingsMARKITUP_PREVIEW_FILTERThis optional setting can be used to override the markup filter used
for the Ajax preview view, if for some reason you need it to be
different from the filter used for rendering markup in aMarkupField. It has the same format asMARKITUP_FILTER; by
default it is set equal toMARKITUP_FILTER.MARKITUP_AUTO_PREVIEWIf set toTrue, the preview window will be activated by
default. Defaults toFalse.JQUERY_URLMarkItUp! requires the jQuery Javascript library.  By default, django-markitup
links to jQuery 2.0.3 at ajax.googleapis.com (via the URL//ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js).  If you
wish to use a different version of jQuery, or host it yourself, set the
JQUERY_URL setting.  For example:JQUERY_URL = 'jquery.min.js'This will use the jQuery available at STATIC_URL/jquery.min.js. A relativeJQUERY_URLis relative toSTATIC_URL.If you include the jQuery library manually in your templates and don’t wantdjango-markitupto include it, setJQUERY_URLtoNone.
CHANGES
=======4.1.0 (2022-08-25)Add support for Django 4.0 & 4.1.4.0.0 (2020.04.06)Drop support for Python 2.7.Drop compatibility with Django < 1.11.3.5.0 (2020.02.09)Add support for Django 1.11 and 2.0.3.0.0 (2016.09.04)Drop support for Python 3.3.Drop compatibility with Django < 1.8.2.3.1 (2016.02.06)Use protocol-less URL for externally hosted jQuery to avoid browser warnings.2.3.0 (2016.01.17)Template tags now support Django’s STATICFILES_STORAGE setting.
Thanks Ivan Ven Osdel for report and fix.Addedrender_withmethod to render a MarkupField with a different filter.Dropped compatibility with Django < 1.7.Compatibility with Django 1.8 and 1.9. Thanks Ivan Ven Osdel for the fixes!Dropped compatibility with Python 2.6.Added support for Python 3.5.2.2.2 (2014.09.08)Adapted MarkupField to work with Django 1.7 migrations. Merge of
BB-15. Thanks Neil Muller for report and fix.2.2.1 (2014.07.15)Fixed regression under Python 2 with MARKITUP_FILTER and
MARKITUP_PREVIEW_FILTER. Thanks Berker Peksag and Neil Muller for the report,
and Neil Muller for the fix.2.2 (2014.07.03)Added Python 3.3+ support. Thanks Berker Peksag.2.1 (2013.11.11)Updated default jQuery version from 1.6 to 2.0.3.FixedMARKITUP_AUTO_PREVIEW; the “fix” in 2.0 was wrong and broke it.2.0 (2013.11.06)FixedMARKITUP_AUTO_PREVIEW; MarkItUp! now observes mousedown events, not
mouseup. Thanks Alexandr Shurigin.Added support for Django 1.6.BACKWARDS-INCOMPATIBLE: Dropped support for Python 2.5 and Django 1.3.1.1.0 (2013.04.26)Updated to MarkItUp! 1.1.14 and fixed compatibility with jQuery 1.9. Thanks
Roman Akinfold!Fixed MarkItUpWidget with custom attrs. Thanks GeyseR.Set previewParserPath dynamically rather than requiring it to be set inset.js.  Thanks Sebastian Brandt.Fixed hidden-widget rendering of aMarkupField. Thanks Aramgutang.Prevented double application of MarkItUp! editor to an
element. Fixes #4. Thanks Rich Leland.Added__len__toMarkupobject to facilitate length and truthiness checks
in templates. Fixes #16. Thanks Edmund von der Burg.1.0.0 (2011.07.11)Removed all compatibility shims for Django versions prior to 1.3, including
all support for static media atMEDIA_URL, static assets undermedia/, and theMARKITUP_MEDIA_URLsetting.Updated to jquery 1.6.Added check to avoid double _rendered fields when MarkupField is used on an
abstract base model class. Fixes #11. Thanks Denis Kolodin for report and
patch.Added compatibility with new AJAX CSRF requirements in Django 1.2.5 and
1.3. Fixes #7. Thanks zw0rk for the report.Added blank=True to MarkupField’s auto-added rendered-field to avoid South
warnings.Django 1.3 & staticfiles compatibility: MARKITUP_MEDIA_URL and jQuery URL
default to STATIC_URL rather than MEDIA_URL, if set.  Static assets now
available under static/ as well as media/.  Thanks Mikhail Korobov.MarkupField.get_db_prep_value updated to take “connection” and “prepared”
arguments to avoid deprecation warnings under Django 1.3.  Thanks Mikhail
Korobov.enforce minimum length of 3 characters for MarkItUp!-inserted h1 and h2
underline-style headers (works around bug in python-markdown).  Thanks
Daemian Mack for the report.0.6.1 (2010.07.01)Added markitup set for reST. Thanks Jannis Leidel.fixed reST renderer to not strip initial headline. Thanks Jannis Leidel.prevent mark_safe from mangling Markup objects.0.6.0 (2010.04.26)remove previously-deprecated markitup_head template tagwrap jQuery usage in anonymous function, to be more robust against other
JS framework code on the page (including other jQuerys).  Thanks Mikhael
Korneev.upgrade to MarkItUp! 1.1.7add render_markup template filterupdate to jQuery 1.4 and MarkItUp! 1.1.6Add auto_preview option.Ajax preview view now uses RequestContext, and additionally passesMARKITUP_MEDIA_URLinto the template context. (Previously,MARKITUP_MEDIA_URLwas passed asMEDIA_URLand
RequestContext was not used). Backwards-incompatible; may require
change to preview template.0.5.2 (2009.11.24)Fix setup.py sotestspackage is not installed.0.5.1 (2009.11.18)Added emptymodels.pyfile somarkitupis properly registered inINSTALLED_APPS. Fixes issue withdjango-staticfilestip not
finding media.0.5 (2009.11.12)AddedMarkupFieldfromhttp://github.com/carljm/django-markupfield(thanks Mike Korobov)Deprecatedmarkitup_headtemplate tag in favor ofmarkitup_media.AddedMARKITUP_MEDIA_URLsetting to override base of relative media
URL paths.0.3 (2009.11.04)added template-tag method for applying MarkItUp! editor (inspired by
django-wysiwyg)0.2 (2009.03.18)initial releaseTODOadd support for multiple markup choices (perhaps integration with
django-markup)link to live demo?