djangosnippets.org: Latest snippets tagged with 'truncate'https://djangosnippets.org/tags/truncate/2012-01-20T05:06:28.675440-06:00Precise truncate chars filter
2012-01-20T05:06:28.675440-06:00davmuzhttps://djangosnippets.org/snippets/2653/<p>Template filter that truncates the text when it exceeds a certain number of characters.
It deletes the last word only if partial.
Adds '...' at the end of the text, only if truncated.</p>
<p>Examples (text == 'Lorem ipsum dolor sit amet', len(text) == 26)</p>
<pre><code>{{ text|truncatewords_by_chars:30 }}
'Lorem ipsum dolor …</code></pre>
Freely redistributableTruncate text to length up until the nearest space
2011-03-05T10:32:17.191274-06:00phektushttps://djangosnippets.org/snippets/2382/<p>This will truncate a long character based on the given length parameter. If the word is cut-off, it will return the string up until the next space. If there are no spaces in the next 5 characters, that should mean a very long word and we should truncate right away.
</p>
Freely redistributableWord-boundary-aware string truncation template filter
2009-05-20T16:37:15.974511-05:00joshohttps://djangosnippets.org/snippets/1516/<p>This is a custom template filter that allows you to truncate a string to a maximum of num characters, but respecting word boundaries. So, for example, if <code>string = "This is a test string."</code>, then <code>{{ string|truncatechars:12 }}</code> would give you "This is a..." instead of "This is a …</p>
Freely redistributableTruncate HTML without breaking tags
2009-05-01T09:17:41.827738-05:00olauhttps://djangosnippets.org/snippets/1477/<p>Put it in appname/templatetags/truncatehtml.py and load it with {% load truncatehtml %}, then for instance {{ some_story|truncatehtml:100 }} to truncate the story to 100 characters.</p>
<p>Tags are not counted in the length given, and character entities count as one character.</p>
<p>The filter should never break an open-tag text close-tag sequence …</p>
Freely redistributableTruncate filter
2009-04-28T08:52:32.094035-05:00zalunhttps://djangosnippets.org/snippets/1471/<p>Truncates string to a given number of characters.</p>
Freely redistributableTruncate string after a given number of chars keeping whole words
2008-12-23T06:27:07.712404-06:00rixhttps://djangosnippets.org/snippets/1259/<p>Truncates a string after a given length, keeping the last word complete.</p>
<p>This filter is more precise than the default <code>truncatewords</code> filter.</p>
<p>Words length vary too much, 10 words may result in 40 or 70 characters, so cutting by character count makes more sense.</p>
<p>There is a <a href="http://ricobl.wordpress.com/2008/12/23/templates-django-filtro-truncatewords-melhorado/">blog post</a> about …</p>
Freely redistributableInline truncate by character number
2008-07-07T04:09:24.180022-05:00esanchezhttps://djangosnippets.org/snippets/860/<p>Sometimes I need to truncate a string after a number of characters, usually to avoid breaking the page layout. When the string we have to truncate is a filename I don't want to hide its extension so a user can easily recognize the file.</p>
<p>My solution is add the ellipsis …</p>
Freely redistributableTruncate words by characters
2008-05-19T19:50:39.843595-05:00trodrigueshttps://djangosnippets.org/snippets/763/<p>This filter truncates words like the original truncate words Django filter, but instead of being based on the number of words, it's based on the number of characters. I found the need for this when building a website where i'd have to show labels on really small text boxes and …</p>
Freely redistributableTruncateChars
2007-10-13T16:02:47.706784-05:00Avealhttps://djangosnippets.org/snippets/444/<p><strong>Usuage:</strong>
{{ comment.comment|truncatechars:20 }}</p>
<p><strong>Why to use:</strong>
First of all, my english isn´t the best. I hope you will understand my text anyways :)</p>
<p>Ok, I had a problem with some "kiddies" posting huge comments like </p>
<p>"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaahhhhhhhhhhhhhhhhhhh this game rooooooooooocks!!!!!!!!!!!!!!!!". </p>
<p>Django has no function to "kill" that huge words within …</p>
Freely redistributabletruncate letters
2007-03-23T00:36:13.966784-05:00trbshttps://djangosnippets.org/snippets/126/<p>filter for truncating strings similar to truncatewords only with letters.</p>
Freely redistributable