-
这类似于
,除了它适用于构建 HTML 片段。所有 args 和 kwargs 在传递给str.format()str.format()之前都通过传递。conditional_escape()对于构建小的 HTML 片段的情况,此函数优于直接使用
%或str.format()的字符串插值,因为它对所有参数应用转义 - 就像模板系统默认应用转义一样。所以,而不是写:
mark_safe("%s <b>%s</b> %s" % ( some_html, escape(some_text), escape(some_other_text), ))您应该改用:
format_html("{} <b>{}</b> {}", mark_safe(some_html), some_text, some_other_text, )这样做的好处是您不需要将
应用于每个参数,并且如果您忘记了一个错误和 XSS 漏洞的风险。escape()请注意,虽然此函数使用
str.format()进行插值,但str.format()提供的一些格式化选项(例如数字格式化)将不起作用,因为所有参数都通过传递,该conditional_escape()(最终)调用conditional_escape()关于值。force_str()
本文介绍 django.utils.html.format_html 的用法。
声明
format_html(format_string, *args, **kwargs)[source]
相关用法
- Python Django format_html_join用法及代码示例
- Python Django format_lazy用法及代码示例
- Python format()用法及代码示例
- Python calendar formatmonth()用法及代码示例
- Python calendar formatyear()用法及代码示例
- Python focus_set() and focus_get()用法及代码示例
- Python dict fromkeys()用法及代码示例
- Python frexp()用法及代码示例
- Python functools.wraps用法及代码示例
- Python functools.singledispatchmethod用法及代码示例
- Python float转exponential用法及代码示例
- Python calendar firstweekday()用法及代码示例
- Python float.is_integer用法及代码示例
- Python filecmp.cmpfiles()用法及代码示例
- Python functools.singledispatch用法及代码示例
- Python fileinput.filelineno()用法及代码示例
- Python fileinput.lineno()用法及代码示例
- Python fileinput.input用法及代码示例
- Python functools.partial用法及代码示例
- Python functools.partialmethod用法及代码示例
- Python fnmatch.fnmatch用法及代码示例
- Python functools.cache用法及代码示例
- Python Django fromfile用法及代码示例
- Python functools.lru_cache用法及代码示例
注:本文由纯净天空筛选整理自djangoproject.com大神的英文原创作品 django.utils.html.format_html。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。
