广告位 |
django模板用slice过滤器限制字数
2021年9月25日 11:31编程 > 2425人已围观
简介 如果" 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无限分类
广告位 |
相关文章
随机图文
-
怎样才能不“死”?将作文写具体并非你想象中的那么难
著名儿童文学作家张秋生写过一首好玩的儿童诗《“死”了十二次》,诗中有两位小朋友,他们谈话不到十分钟,整整“死”了十二次。这是怎么回事呢?... -
为人随和一点,我们会感受到生活更加美好
有人说,随和就是顺从众议,不固执己见;有人说,随和就是不斤斤计较,为人和蔼;还有人说,随和其实就是傻,就是老好人,就是没有原则。那么,随和到底是什么?... -
卖货,社群猛于社区
中秋节,王旭收到了期盼已久的添可吸拖一体机2.0LED(以下简称“添可”),官方旗舰店活动价3890元,而王旭在社群团购只花了3099元。 节省800的原因是一周前,王旭收到快递后在朋友圈的一个开箱视频分享。... -
“拼购”为何成为电商博弈的又一杀手锏?
10月19日,京东宣布今年11.11大促将首次推出“京东拼购”等玩法。10月19日,美团点评官方微信小程序也正式上线了“好货拼团”板块。 事实上,此前已经有包括阿里、苏宁、今日头条等多家互联网巨头切入了社交拼团业务。而这一切,在笔者看来,巨头们完全已经将拼购当成了获取用户流量的下一个杀手锏。...