Django - 使用 Bleach 渲染 Markdown
Django - Rendering Markdown Sanitizied with Bleach
当我在不使用漂白剂的情况下执行 markdown(text) 时,我得到了想要的结果(原始):
<p>blah</p>
它正确显示为:
blah
其中 "p" 标记正确呈现为段落块。
当我执行 bleach.clean(markdown.markdown(text)) 时,我得到 (raw):
<p>blah</p>
它错误地显示为:
<p>blah</p>
其中 "p" 标签是文本的一部分,而不是 HTML 段落块。
您需要将 bleach
ed 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
等等
当我在不使用漂白剂的情况下执行 markdown(text) 时,我得到了想要的结果(原始):
<p>blah</p>
它正确显示为:
blah
其中 "p" 标记正确呈现为段落块。
当我执行 bleach.clean(markdown.markdown(text)) 时,我得到 (raw):
<p>blah</p>
它错误地显示为:
<p>blah</p>
其中 "p" 标签是文本的一部分,而不是 HTML 段落块。
您需要将 bleach
ed 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
等等