广告位 |
django模板用slice过滤器限制字数
2021年9月25日 11:31编程 > 2709人已围观
简介 如果" my_variable"是字符串,则可以利用切片过滤器,该过滤器将字符串视为字符列表。如果是一组单词,则大致等同于 truncatewords -但这听起来不太像您的需要。 truncatewords 还添加了省略号 ... 截断结果的结尾。...
如果" my_variable"是字符串,则可以利用切片过滤器,该过滤器将字符串视为字符列表。如果是一组单词,则大致等同于 truncatewords -但这听起来不太像您的需要。
truncatewords 还添加了省略号 ... 截断结果的结尾。
用法类似于
{{my_variable | slice:":255"}}
I am trying to output the first 255 characters of a description on a list of items and am looking for a method to get that.
Example: I have a variable that contains 300 or so characters.
I call that variable like this, {{ my_variable|characterlimit:255 }}
and it would return only the first 255 characters of that variable.
If this tag doesn't exist, I will simply create it (and suggest that it goes into django), but I wanted to make sure it didn't before I took the time to do that. Thanks!
解决方案
If the "my_variable" is a string, you can take advantage of the slice filter, which treats the string as a list of characters. If it's a set of words, the rough equivilant is truncatewords - but that doesn't quite sound like your need.
truncatewordsalso adds an ellipsis ... at the end of the truncated result.
Usage would be something like
{{ my_variable|slice:":255" }}
上一篇: django模板中自动加载static
下一篇: django无限分类
广告位 |
相关文章
随机图文
-
商家增长“拐点”在哪里
商品与消费者之间,有着“一体两翼”的关系。“一体”是两者链路之间需要有运营桥梁,而“两翼”便是数据和流量,这就需要线下商家在运营中一手抓数据、一手抓流量。... -
django模板中自动加载static
'builtins': ['django.templatetags.static'] # 模板中自动加载static,不需要写 {% load static %}... -
生活是根绳子,总是牵着我们的鼻子走。
一、 太多的选择,等于没有选择;什么都想要,等于什么都不想要。人只有懂得取舍,才可以让自己活到正确的位置上。 二、 生活是根绳子,总是牵着我们的鼻子走。为了生活,我们忍让、退缩、扭曲甚至出卖自己。无... -
抖音电商的“独立宣言”
据说,抖音电商海外版产品已邀请到不少华东地区海外大卖家进行内测,预计10月份正式推出。而该项目在字节跳动内部的代号为 “麦哲伦 XYZ ”, 由周翀带队,向抖音电商总裁康泽宇汇报,后者主抓字节电商业务的国际化。...