Limit number of characters with Django Template filter

后端 未结 6 1114
盖世英雄少女心
盖世英雄少女心 2021-02-01 12:34

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

6条回答
  •  滥情空心
    2021-02-01 13:09

    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" }}
    

提交回复
热议问题