广告位 |
django模板用slice过滤器限制字数
2021年9月25日 11:31编程 > 1362人已围观
简介 如果" 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无限分类
广告位 |
相关文章
随机图文
-
雷军怎样在小米诞生前夜决定拯救金山
据雷军回忆,“2011年我们小米已经创办了一年多时间,那个时候我们正在紧锣密鼓地开发小米手机第一代,非常非常忙。金山的两位老板张旋龙、求伯君来找我,当时金山遭遇了巨大的危机,他们希望我重回金山。... -
降维打击,今年就业形势究竟有多难?
近日,武汉东湖高新区社会事务局对社区干事免笔试人员名单进行公示,24名高学历人才赫然在列,引人瞩目。 这24人中,有5名博士、19名硕士,其中博士的专业分别是环境科学、材料科学与工程、作物环境及生态、生物化学与分子生物学、行星地质与比较行星学。... -
一些噎死人的句子,句句精辟犀利
不管你多大年龄,是什么性格的人,只要你有太容易相信人的特点,你就拥有了绝对死穴。 01 见识越广,计较越少,经历越多,抱怨越少,越闲,越矫情。 02 善良和爱都是免费的,但不是廉价的。 你的善良... -
仪表企业的宣传册样本
仪表企业对宣传册样本的形象有怎样的要求?行业类别不同,企业理念不同,对宣传册所反映的需求区别是很大的。简洁、留白的宣传画册是目前审美主流,那么仪表企业的宣传册是否必须遵循这一趋势?设计风格是多样性的,比如仪表产品的简约排版等等,勘破表象直指本质,适合仪表企业需求的宣传册设计,不千篇一律才是上品。...