2016-12-03 220 views
2

在我的应用程序队/ templatetags/teams_extras.py我有这样的过滤器Django过滤器。 is_safe不起作用

from django import template 

register = template.Library() 

@register.filter(is_safe=True) 
def quote(text): 
    return "« {} »".format(text) 

所以我用它为我的观点队/模板/团队/ show.html

{% extends './base.html' %} 
{% load static %} 
{% load teams_extras %} 
... 
<b>Bio :</b> {{ team.biography|quote }} 
... 

但是,这是我的网页上的结果:

&laquo; <p>The Miami Heat are an American professional basketball team based in Miami. The Heat compete in the National Basketball Association as a member of the league's Eastern Conference Southeast Division</p> &raquo; 

为什么? 谢谢

+0

尝试这个'返回'«{}»“.format(text)' –

+0

我有这个:««

迈阿密热火队是一支美国职业篮球队,总部设在迈阿密。热火在美国国家篮球协会参加东部联盟东南联盟成员

»' – Alexandre

+0

您可以使用过滤器安全[doc](https://docs.djangoproject.com/en/1.10/ref/templates/ builtins /#safe) –

回答

4

文件says

这个标志告诉Django,如果一个“安全”的字符串传递到您的过滤器,结果仍然是“安全的”,如果一个非安全字符串传递如果有必要的话,Django会自动转义它。

所以试图通过安全值到过滤器:

{{ team.biography|safe|quote }} 

或用户mark_safe

from django.utils.safestring import mark_safe 

@register.filter() 
def quote(text): 
    return mark_safe("&laquo; {} &raquo;".format(text)) 

和:

{{ team.biography|quote }} 

这应该工作。

+0

这是工作!谢谢 ;) – Alexandre