[英]django templates string formatting
我想填補和削減太長的字符。 所以我用了stringformat。 在此示例中,我希望我的字符串長度為10個字符,並在必要時加填充。 我的Django模板中有這段代碼
{{ "my too long or too short string"|stringformat:":10" }}
但是django什么也沒輸出。
使用內部truncatechars
模板標簽
{{ "my too long or too short string"|truncatechars:10 }}
Django 1.10
, 此處文檔,
使用truncatechars
, rjust
在一起。
的index.html
<p>Non formated string</p>
<p>{{ "Truncatechars (include ... length)"|truncatechars:10}}</p>
<p>{{ "Truncatechars with rjust"|truncatechars:10|rjust:"30" }}</p>
<p>{{ "Only rjust"|rjust:"30" }}</p>
產量
模板呈現正確的HTML,如下所示。
請注意white-space: pre;
css屬性代表空白。
p { white-space: pre; }
<p>Non formated string</p> <p>Truncat...</p> <p> Truncat...</p> <p> Only rjust</p>
關於空白,請參見此處
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.