Troubleshooting Error Using Label Filter in Django Template

When using Markdown libraries, I seem to get the following error:

Error in "markdown" filter: Django does not support versions of Python markdown library <2.1.

As an example, this happens in a tag, for example:

{{ticket.get_description|markdown:"safe,footnotes,tables"}} 

The get_description function get_description defined in the Ticket model. We recently updated Django 1.5, and the code was written before Django 1.4. I also updated the Markup library to 2.3.1, but the problem still persists. I also cleaned up the old .pyc files to be sure.

From what I read, the django.contrib.markup libraries django.contrib.markup out of date . So what is the proposed solution / alternative?

+4
source share
2 answers

one idea is to install python markdown2 library see here then you create your decorator

 import markdown2 .. all other imports needed.. register = template.Library() @register.filter(is_safe=True) @stringfilter def markdown2(value): return mark_safe(markdown2.markdown(force_unicode(value),safe_mode=True,enable_attributes=False)) 

then you use it

 {% load myapp_markup %} {{ value|markdown2 }} 

adpated code (and not verified) from here

+4
source

Just an update:

My decorator looks like this:

 import markdown2 from django import template from django.template.defaultfilters import stringfilter from django.utils.encoding import force_unicode from django.utils.safestring import mark_safe register = template.Library() @register.filter(is_safe=True) @stringfilter def convertTxt(value): return mark_safe(markdown2.markdown(force_unicode(value))) register.filter('convertTxt', convertTxt) 

In addition, I noticed that it is not wise to call your module or your markdown2 method :)

+2
source

Source: https://habr.com/ru/post/1482131/


All Articles