广告位 |
django模板用slice过滤器限制字数
2021年9月25日 11:31编程 > 1298人已围观
简介 如果" 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无限分类
广告位 |
相关文章
随机图文
-
仪表企业的宣传册样本
仪表企业对宣传册样本的形象有怎样的要求?行业类别不同,企业理念不同,对宣传册所反映的需求区别是很大的。简洁、留白的宣传画册是目前审美主流,那么仪表企业的宣传册是否必须遵循这一趋势?设计风格是多样性的,比如仪表产品的简约排版等等,勘破表象直指本质,适合仪表企业需求的宣传册设计,不千篇一律才是上品。... -
生鲜电商接棒,“线上卖花”生意又好做了?
谁能想到,各大垂直鲜花电商在这几年间花钱费力,好不容易才完成了部分定向人群的“鲜花日常购”目标,却被后入场的生鲜电商们用“白菜价”鲜花摘了桃子——据报道,某生鲜平台的鲜花日平均销量已达20万支。... -
女人最容易被男人哄骗的三句话
人的欲望总是被人们所忽视,认为欲望一旦有了苗头就要掐灭,不能让它继续生长,否则可能会引起不良的影响;很多人觉得欲望是坏事,因此常常把欲望压抑在心底里,不轻易的表达出来,很多人却认为欲望要得到释放后才能让人变得正常,否则积压久了就会对健康有影响了。... -
thinkphp模型数据的新增
模型数据的新增和数据库的新增数据有所区别,数据库的新增只是单纯的写入给定的数据,而模型的数据写入会包含修改器、自动完成以及模型事件等环节,数据库的数据写入参考数据库章节。...