如何使用 truncatewords 在 Django 模板中添加省略号而不需要最后的空格?

发布于 2024-09-30 00:50:38 字数 175 浏览 1 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一抹淡然 2024-10-07 00:50:38

Djangosnippets这个看起来很整洁

# From http://djangosnippets.org/snippets/1259/

from django import template

register = template.Library()

@register.filter
def truncatesmart(value, limit=80):
    """
    Truncates a string after a given number of chars keeping whole words.

    Usage:
        {{ string|truncatesmart }}
        {{ string|truncatesmart:50 }}
    """

    try:
        limit = int(limit)
    # invalid literal for int()
    except ValueError:
        # Fail silently.
        return value

    # Make sure it's unicode
    value = unicode(value)

    # Return the string itself if length is smaller or equal to the limit
    if len(value) <= limit:
        return value

    # Cut the string
    value = value[:limit]

    # Break into words and remove the last
    words = value.split(' ')[:-1]

    # Join the words and return
    return ' '.join(words) + '...'

There are a bunch of template filters at Djangosnippets, and this one looks pretty neat:

# From http://djangosnippets.org/snippets/1259/

from django import template

register = template.Library()

@register.filter
def truncatesmart(value, limit=80):
    """
    Truncates a string after a given number of chars keeping whole words.

    Usage:
        {{ string|truncatesmart }}
        {{ string|truncatesmart:50 }}
    """

    try:
        limit = int(limit)
    # invalid literal for int()
    except ValueError:
        # Fail silently.
        return value

    # Make sure it's unicode
    value = unicode(value)

    # Return the string itself if length is smaller or equal to the limit
    if len(value) <= limit:
        return value

    # Cut the string
    value = value[:limit]

    # Break into words and remove the last
    words = value.split(' ')[:-1]

    # Join the words and return
    return ' '.join(words) + '...'
百善笑为先 2024-10-07 00:50:38

这也可行:

{{ value|truncatewords:3|slice:"-4" }}...

基本上,只需切掉最后 4 个字符(椭圆加空格),然后将其添加回来(不带空格)!

巧妙的是,通过这种方法,您还可以用任何您想要的内容来结束截断。

This will also work:

{{ value|truncatewords:3|slice:"-4" }}...

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文