Adding Markdown Back to Django (Posted on June 1st, 2013)

If you head over to the comment section you'll notice that my comments section now supports Markdown. I also upgraded to Django 1.5 which deprecates the markup module. If you grab the Markdown module you can pretty much add support back for markdown in one line of code.

import markdown
form.content = markdown.markdown(form.content, safe_mode='escape')
form.save()

If you prefer to use a template filter rather than saving the markdown text you could do something like this:

from django import template
import markdown

register = template.Library()

@register.filter
def markdown_processor(text):
    return markdown.markdown(text, safe_mode='escape')

#Use it like this in your template
#{{var|markdown_processor}}

You can read more about the different modes in the Markdown library documentation. Overall though I think you'll find it relatively easy to switch from Django's module to this one. Enjoy!

Tags: Django