如何使用截断词在没有最终空格的Django模板中添加省略号?

时间:2022-10-15 14:31:14

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...'

truncatewords过滤器在省略号之前插入一个空格。就像在'一个精美的假期食谱书......'与期望的'一个精美的假期食谱书......'

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.

是否有一种简单的方法可以让这个过滤器不在那里放置空间?我可以很容易地在视图中处理这个问题,但更愿意在模板中进行 - 最好不要创建自定义过滤器。欢迎任何建议。

2 个解决方案

#1


6  

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

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) + '...'

#2


2  

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!

基本上,只需切掉最后4个字符(椭圆加空格),然后在没有空格的情况下将其添加回来!

The neat thing is, with this method you can also end your, uh, truncation with anything you'd like.

整洁的是,用这种方法你也可以用你想要的任何东西来结束你的呃截断。

#1


6  

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

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) + '...'

#2


2  

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!

基本上,只需切掉最后4个字符(椭圆加空格),然后在没有空格的情况下将其添加回来!

The neat thing is, with this method you can also end your, uh, truncation with anything you'd like.

整洁的是,用这种方法你也可以用你想要的任何东西来结束你的呃截断。