[Answer]-TypeError using custom template tag

1๐Ÿ‘

โœ…

add this to INSTALLED_APPS

'django.contrib.markup',

copy markdown(http://pypi.python.org/pypi/Markdown) to your django project directory

then use

{% load markup %}
<div class="span8" id="editor2">
      {{ selected_campaign.description|markdown:"safe" }}
</div>

Update:

django.contrib.markup is deprecated in Django 1.5. Here is a simple replacement for the markdown filter.

remove line 'django.contrib.markup', from INSTALLED_APPS

steps to create a template tag:

  • add a folder templatetags in any of your app folder.
  • inside templatetags folder add an empty file __init__.py

add markup.py inside templatetags with these codes:

from django import template
from django.utils.safestring import mark_safe
import markdown as mkdn

register = template.Library()     
@register.filter
def markdown(value,smode=None):
    return mark_safe(mkdn.markdown(value, safe_mode='escape'))
๐Ÿ‘คsuhailvs

0๐Ÿ‘

Well I do not why but markdown seems does not work inside a function in django for example I typed this in django shell (python manage.py shell):

from markdown import markdown

def yes():
     return markdown("YES")

and it gives me the next error:

NameError: global name 'markdown' is not defined

and this seems to work

def yes():
     from markdown import markdown
     return markdown("YES")

outside django shell the first method works correctly hope this helps!

Leave a comment