广告位 |
django模板用slice过滤器限制字数
2021年9月25日 11:31编程 > 627人已围观
简介 如果" 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无限分类
广告位 |
相关文章
随机图文
-
画册封面设计的六种常用表现形式
画册设计是平面设计师会经常遇到的工作,而画册的封面又是画册最为重要的部分,它起着概括画册内容、奠定画册调性、彰显画册品质,以及吸引读者的作用,而且画册设计对设计师的排版能力、构图能力、色彩搭配能力、审美、材质和工艺的运用都有不小的要求,值得深入学习和研究,所以葱爷决定专门写一篇文章,概括封面设计最常用的几种形式。... -
他明白了为何古人皆要戒酒
他明白了为何古人皆要戒酒消愁,他明白为何达官显贵皆要放浪青楼。也许本是他的错,那年年少,错把痴情真心付之东流。... -
联商首页资讯动态新零售正文 抖音“防沉迷”,能有多大用?
一提到抖音,江海就忍不住叹气。 他家孩子7岁,刚上一年级就迷上了抖音。让他放下手机来吃饭,得到的回复总是,“等一下,马上就来”,接着就一直磨蹭,遇到好笑的,就“咯咯咯”傻笑。放学回家刷,晚上睡前刷,吃饭的时候也刷,“如果刷牙有这么自觉就好了”,他说。... -
django模板中自动加载static
'builtins': ['django.templatetags.static'] # 模板中自动加载static,不需要写 {% load static %}...