如何使用 truncatewords 在 Django 模板中添加省略号而不需要最后的空格?
truncatewords 过滤器在省略号前插入一个空格。如, “一本精美的节日食谱书......”
与期望的相比
“一本精美的节日食谱书……”
有没有一种简单的方法可以让这个过滤器不留空格?我可以很轻松地在视图中处理此问题,但更喜欢在模板中执行此操作 - 理想情况下无需创建自定义过滤器。欢迎任何建议。
The truncatewords filter inserts a space before the elipsis. As in,
'A fine holiday recipe book of ...'
vs. the desired
'A fine holiday recipe book of...'
Is there an easy way to get this filter to not put a space there? I could take care of this in the view pretty easily, but would prefer to do it in the template - ideally without creating a custom filter. Any suggestions are welcome.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Djangosnippets 和 这个看起来很整洁:
There are a bunch of template filters at Djangosnippets, and this one looks pretty neat:
这也可行:
基本上,只需切掉最后 4 个字符(椭圆加空格),然后将其添加回来(不带空格)!
巧妙的是,通过这种方法,您还可以用任何您想要的内容来结束截断。
This will also work:
Basically, just slice off the last 4 characters (ellipse plus space), and then add it back without the space!
The neat thing is, with this method you can also end your, uh, truncation with anything you'd like.