在Django模板中使用降价过滤器时出现故障排除错误 [英] Troubleshooting error when using markdown filter in Django template
问题描述
当使用Markdown库时,我似乎收到以下错误:
When using the Markdown libraries I seem to get the following error:
'markdown'过滤器中的错误:Django不支持版本的
Python标记库< 2.1。
Error in 'markdown' filter: Django does not support versions of the Python markdown library < 2.1.
例如,它发生在一个标签上,例如:
As an example, it occurs on a tag such as:
{{ticket.get_description|markdown:"safe,footnotes,tables"}}
get_description
函数在 Ticket
模型中定义。我们最近升级到了Django 1.5,代码是Django 1.4之前的。我还将Markup库升级到了2.3.1,但问题仍然存在。我也清除了旧的.pyc文件,只是为了确保。
The get_description
function is defined in the Ticket
model. We've upgraded to Django 1.5 recently and the code was written pre Django 1.4. I've also upgraded the Markup library to 2.3.1 but the problem still persists. I've also cleared old .pyc files, just to be sure.
从我所看到的, django.contrib.markup
库已被弃用。那么,建议的解决方案/替代方案是什么?
From what I've read, the django.contrib.markup
libraries have been deprecated. So, what would the suggested solution/alternative be?
推荐答案
一个想法是安装python的markdown2库在这里
然后你创建你的装饰器
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))
然后你使用它
{% load myapp_markup %}
{{ value|markdown2 }}
代码被填充(而不是测试)从此处
code is adpated (and not tested) from here
这篇关于在Django模板中使用降价过滤器时出现故障排除错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!