-
這類似於
,除了它適用於構建 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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。
