Skip to content

Instantly share code, notes, and snippets.

@curtmerrill
Created November 7, 2013 15:48
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save curtmerrill/7356817 to your computer and use it in GitHub Desktop.
Save curtmerrill/7356817 to your computer and use it in GitHub Desktop.
Markdown template tag for Django
# Creates a template tag so you can use Markdown in textfields
#
# REQUIRES:
# - markdown 2 -- pip install markdown2
#
# USAGE:
# - Create a folder in your app called "templatetags"
# - Save this file and an __init__.py in the folder
# - In your template, add "{% load markdown_tag %}" at top
# - {{ field|md }} will output escaped markup
# - {{ field|md|safe }} outputs "safe" markup
from django import template
from django.template.defaultfilters import stringfilter
import markdown2
register = template.Library()
@register.filter(name="md")
@stringfilter
def markdown(value):
return markdown2.markdown(value)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment