Skip to content

Instantly share code, notes, and snippets.

@soar soar/
Last active Apr 1, 2018

What would you like to do?
Wagtail Markdown - simplest way
# django
from django import forms
from django.contrib.staticfiles.templatetags.staticfiles import static
from django.forms import Media, widgets
from django.utils.functional import cached_property
# wagtail
from wagtail.utils.widgets import WidgetWithScript
from wagtail.wagtailcore import blocks
# markdown
import markdown as MarkdownLibrary
class MarkdownBlock(blocks.TextBlock):
def field(self):
field_kwargs = {
'widget': MarkdownWidget(),
return forms.CharField(**field_kwargs)
class Meta:
icon = 'code'
template = 'blocks/markdown.html'
class MarkdownWidget(WidgetWithScript, widgets.Textarea):
def render_js_init(self, id_, name, value):
jsinit = """
if (window.SimpleMDEInstances == null) {{
window.SimpleMDEInstances = [];
sm = new SimpleMDE({{
element: document.getElementById("{id!s}"),
forceSync: true,
spellChecker: false
return jsinit.format(id=id_)
def media(self):
js = [
css = {'all': [
return Media(js=js, css=css)
# django
from django import template
from django.utils.safestring import mark_safe
# markdown
import markdown as MarkdownLibrary
register = template.Library()
def markdown_filter(value):
return mark_safe(MarkdownLibrary.markdown(

This comment has been minimized.

Copy link
Owner Author

commented Jun 9, 2016

SimpleMDE required - it can be downloaded here:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.