【问题标题】:Django - Rendering Markdown Sanitizied with BleachDjango - 使用 Bleach 渲染 Markdown
【发布时间】:2016-10-11 23:12:05
【问题描述】:
当我做降价(文本)时,没有漂白,我得到了想要的结果(原始):
<p>blah</p>
它正确显示为:
blah
“p”标签正确呈现为段落块。
当我执行bleach.clean(markdown.markdown(text)) 时,我得到(原始):
<p>blah</p>
它错误地显示为:
<p>blah</p>
其中“p”标签是文本的一部分,而不是 HTML 段落块。
【问题讨论】:
标签:
python
django
html
markdown
【解决方案1】:
您需要将bleached HTML 标记为安全
from django.utils.safestring import mark_safe
...
return mark_safe(bleach.clean(markdown.markdown(text)))
但是,也有 django-bleach 提供与 Django 的集成和现成的标签以在 Django 中使用漂白剂。
{% load markdown_deux_tags bleach_tags %}
{{ view_user.profile.about|markdown:"user"|bleach }}
在 settings.py 你可以告诉 django-bleach 哪些标签是可以的
BLEACH_ALLOWED_TAGS = ['h1', 'h2', 'p', 'b', 'i', 'strong', 'a']
BLEACH_ALLOWED_ATTRIBUTES = ['href', 'title', 'style']
BLEACH_ALLOWED_STYLES = ['font-family', 'font-weight']
BLEACH_STRIP_TAGS = True
等等