django 自定义模板标签

故事的背景比较复杂,框架用的django,后台用的simple ui,当我在往前端嵌入echarts的时候发现自定义标签返回的list 里面的单引号进行了自动转义 ,变成了'

 

具体可以参考:https://docs.djangoproject.com/zh-hans/3.2/howto/custom-template-tags/#filters-and-auto-escaping

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=True)
def initial_letter_filter(text, autoescape=True):
    first, other = text[0], text[1:]
    if autoescape:
        esc = conditional_escape
    else:
        esc = lambda x: x
    result = '<strong>%s</strong>%s' % (esc(first), esc(other))
    return mark_safe(result)

  

通过make_safe来取消自动转义。

posted @ 2023-01-29 16:37  JvvYou  阅读(21)  评论(0编辑  收藏  举报