广告位 |
django模板用slice过滤器限制字数
2021年9月25日 11:31编程 > 990人已围观
简介 如果" 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无限分类
广告位 |
相关文章
随机图文
-
申请Let's Encrypt永久免费SSL证书
Let's Encrypt 发布的 ACME v2 现已正式支持通配符证书,接下来将为大家介绍怎样申请... -
django2集成DjangoUeditor富文本编辑器
富文本编辑器,在web开发中可以说是不可缺少的。django并没有自带富文本编辑器,因此我们需要自己集成,在这里推荐大家使用DjangoUeditor,因为DjangoUeditor封装了我们需要的一些功能如文件上传、在后台和前台一起使用等,非常方便。... -
苹果iPhone 13系列等新品正式迎来发售
苹果iPhone 13系列、新款iPad和iPad mini等新品正式迎来发售。由于时差原因,全球部分地区用户已“提前”拿到iPhone 13系列新品。 目前,部分iPhone 13 预购用户已可在 Apple Store 应用看到订单状态更改为“准备取货”。天猫、京东等电商平台购买的手机已经陆续发货,预计在明后两天就能收到。... -
中国为何能成为五常之一?原来有三大杀手锏,难怪印度有苦难言
最近频频上热搜的印度可谓是红极一时,说到印度,外界的初印象无外乎“摔机大户”“人傻钱多”这些极具调侃标签,当然不出意外印度总是用实际行动来印证外界传言。前段时间,印度更是大言不惭表示,五常应该将英国踢出局,好让自己补位...