gpt4 book ai didi

python - Django 过滤器。 is_safe 不起作用

转载 作者:行者123 更新时间:2023-11-28 22:32:25 25 4
gpt4 key购买 nike

在我的应用程序 teams/templatetags/teams_extras.py 我有这个过滤器

from django import template

register = template.Library()

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

所以我将它用于我的 View teams/templates/teams/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; 

为什么?谢谢

最佳答案

文档 says :

This flag tells Django that if a “safe” string is passed into your filter, the result will still be “safe” and if a non-safe string is passed in, Django will automatically escape it, if necessary.

所以尝试将安全值传递到您的过滤器中:

{{ 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 }}

这应该有效。

关于python - Django 过滤器。 is_safe 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40948075/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com