Django模板过滤器转义

时间:2015-09-01 19:02:25

标签: django django-templates

我有一个自定义过滤器,它接受一个字符串并使其成为我正在使用的工具提示库的相应属性。它适用于OpenTip,但我转换为使用Bootstrap中的工具提示库。

这是我的过滤器:

from django import template
from django.utils.html import conditional_escape
from django.utils.safestring import mark_safe

register = template.Library()


@register.filter(needs_autoescape=False, is_safe=True)
def tooltip(value, autoescape=False):
    """
        Filter to turn some text into the tag that the tooltip library uses -
        Written as a filter so we can switch from one tooltip library to
        another
    """
    if autoescape:
        esc = conditional_escape
    else:
        esc = lambda x: x
    if value is not None and len(value) > 0:
        retval = 'data-toggle="tooltip" data-html="true" ' +\
            'rel="tooltip" title="%s"' % esc(value)
        return mark_safe(retval)
    else:
        return ''

以下我在模板中使用它:

<form id="filter" name="filter" method="post"
class="form-inline">
{% csrf_token %}
    <label for="filterText">Filter Query:</label>
    <input type="text" id="current_filter" name="current_filter" value="{{current_filter}}" placeholder="Filter" class="span8"/>
    <i class="icon-question-sign"
    {{"Filters -<br>requester: [[first] [last]]|[windows_id]<br>client: [[first] [last]]|[windows_id]<br>approver:  [[first] [last]]|[windows_id]<br>worker: [[first] [last]]|[windows_id]<br>ticket: [id]<br>status: [open]|[closed]|[hold]<br>type: [termination]|[extension]|[access]|[password]|baskets]<br>item: [name for category/item/attribute inventory]<br>since: [mm/dd/yyyy]|[yyyy-mm-dd]<br>before: [mm/dd/yyyy]|[yyyy-mm-dd]<br>All searchs are AND with comma delimiting"|tooltip}}></i>
    <input type="submit" name="btnSubmit" class="btn" value="Filter"/>
    <input id="filter_reset" type="button" name="filter_reset" class="btn" value="Clear existing filters"/>
</form>
{% endif %}

但是工具提示并没有处理html,当我进入Firebug并剪切并粘贴html时,尽管我用mark_safe标记了它,但看起来有些事情正在逃避:

<form class="form-inline" method="post" name="filter" id="filter">
<input type="hidden" value="dpuAc9GNUQtvGG5wYzrWsG2Vpu5i7PWJ" name="csrfmiddlewaretoken">
    <label for="filterText">Filter Query:</label>
    <input type="text" class="span8" placeholder="Filter" value="" name="current_filter" id="current_filter">
    <i title="Filters -&lt;br&gt;requester: [[first] [last]]|[windows_id]&lt;br&gt;client: [[first] [last]]|[windows_id]&lt;br&gt;approver:  [[first] [last]]|[windows_id]&lt;br&gt;worker: [[first] [last]]|[windows_id]&lt;br&gt;ticket: [id]&lt;br&gt;status: [open]|[closed]|[hold]&lt;br&gt;type: [termination]|[extension]|[access]|[password]|baskets]&lt;br&gt;item: [name for category/item/attribute inventory]&lt;br&gt;since: [mm/dd/yyyy]|[yyyy-mm-dd]&lt;br&gt;before: [mm/dd/yyyy]|[yyyy-mm-dd]&lt;br&gt;All searchs are AND with comma delimiting" rel="tooltip" data-html="true" data-toggle="tooltip" class="icon-question-sign">
    </i>
    <input type="submit" value="Filter" class="btn" name="btnSubmit">
    <input type="button" value="Clear existing filters" class="btn" name="filter_reset" id="filter_reset">
</form>

如何将该过滤器文本中的html放入页面而不进行转义?

2 个答案:

答案 0 :(得分:2)

尝试使用safe

{{"Filters -<br>requester: [[first] [last]]|[windows_id]<br>client: [[first] [last]]|[windows_id]<br>approver:  [[first] [last]]|[windows_id]<br>worker: [[first] [last]]|[windows_id]<br>ticket: [id]<br>status: [open]|[closed]|[hold]<br>type: [termination]|[extension]|[access]|[password]|baskets]<br>item: [name for category/item/attribute inventory]<br>since: [mm/dd/yyyy]|[yyyy-mm-dd]<br>before: [mm/dd/yyyy]|[yyyy-mm-dd]<br>All searchs are AND with comma delimiting"|safe|tooltip}}

或者您可以尝试从esc代码中删除tooltip

修改

我刚刚意识到你要做的事情。 你不能将html放在tooltip的bootstrap中,它只是一个明文特征data-html="true"允许它包含html内容。您也可以使用popover。仍应使用上面的safe过滤器来禁用html转义。

答案 1 :(得分:0)

事实证明问题比我想象的要愚蠢得多 - 我的<script>标签的顺序错误,所以我得到的是jQuery-UI工具提示,而不是Bootstrap工具提示和jQuery-UI工具提示不支持html。