首先,我要说@DNS的答案是正确的,为什么空格没有显示出来。
考虑到这一点,此模板过滤器会将字符串中的所有空格替换为
用法:
{{ "hey there world"|spacify }}
输出将是
hey there world
这是代码:
from django.template import Libraryfrom django.template.defaultfilters import stringfilterfrom django.utils.html import conditional_escapefrom django.utils.safestring import mark_safeimport reregister = Library()@stringfilterdef spacify(value, autoescape=None): if autoescape: esc = conditional_escape else: esc = lambda x: x return mark_safe(re.sub('s', '&'+'nbsp;', esc(value)))spacify.needs_autoescape = Trueregister.filter(spacify)
有关模板过滤器如何工作以及如何安装的注释,请参阅docs。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)