Troubleshooting error when using markdown filter in Django template

前端 未结 2 1812
长发绾君心
长发绾君心 2021-02-06 15:10

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

Error in \'markdown\' filter: Django does not support versions of the Python markdo

相关标签:
2条回答
  • 2021-02-06 15:58

    one idea is to install markdown2 library of python 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 }}
    

    code is adpated (and not tested) from here

    0 讨论(0)
  • 2021-02-06 16:12

    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)
    

    Also, I've noticed that it is not prudent to name your module or your method markdown2 :)

    0 讨论(0)
提交回复
热议问题