django-markdownify

raw JSON →
0.9.6 verified Mon Apr 27 auth: no python

Markdown template filter for Django using the Python Markdown library. The current version is 0.9.6, with a release cadence of roughly once per year. It provides a simple template filter to render Markdown text as HTML, with optional parameters for allowed tags, attributes, and extensions.

pip install django-markdownify
error TemplateSyntaxError: 'markdownify' is not a registered tag library. Must be one of: ...
cause Missing app in INSTALLED_APPS or using wrong template tag module name.
fix
Add 'django_markdownify' to INSTALLED_APPS and use {% load markdownify %} in templates.
error django.template.exceptions.TemplateSyntaxError: Invalid filter: 'markdownify'
cause Template tag module not loaded or filter not available.
fix
Ensure {% load markdownify %} is present in the template and the app is in INSTALLED_APPS.
error ImportError: cannot import name 'markdownify' from 'markdownify'
cause Incorrect import path in Python code (not a template).
fix
Import the filter via: from django_markdownify.templatetags.markdownify import markdownify
gotcha The template tag module is named 'markdownify' but the app is 'django_markdownify'. Always use {% load markdownify %} in templates.
fix Use {% load markdownify %} in your templates.
deprecated In version 0.9.0, the 'safe' argument was removed. Use 'strip' and whitelist options instead.
fix Remove 'safe' from filter usage and configure via MARKDOWNIFY settings.
breaking Version 0.9.0 changed the filter name from 'markdown' to 'markdownify'. Templates using {% load markdown %} will break.
fix Replace {% load markdown %} with {% load markdownify %} and use {{ text|markdownify }}.

Basic setup to use the markdownify filter in Django templates.

# 1. Install: pip install django-markdownify
# 2. Add to INSTALLED_APPS:
INSTALLED_APPS = [
    ...
    'django_markdownify',
]

# 3. In a template:
{% load markdownify %}
{{ text|markdownify }}

# Optional settings:
MARKDOWNIFY = {
    "default": {
        "MARKDOWN_EXTENSIONS": [
            "markdown.extensions.extra",
            "markdown.extensions.codehilite",
        ],
        "STRIP": False,
        "WHITELIST_TAGS": ["a", "code", "pre"],
        "WHITELIST_ATTRS": ["href", "class"],
    }
}