<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: A Simple Django Truncate Filter</title>
	<atom:link href="http://w.holeso.me/2008/08/a-simple-django-truncate-filter/feed/" rel="self" type="application/rss+xml" />
	<link>http://w.holeso.me/2008/08/a-simple-django-truncate-filter/</link>
	<description>A blog about Python, Django, and other things</description>
	<lastBuildDate>Thu, 15 Sep 2011 17:53:03 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
	<item>
		<title>By: Paul</title>
		<link>http://w.holeso.me/2008/08/a-simple-django-truncate-filter/#comment-23204</link>
		<dc:creator>Paul</dc:creator>
		<pubDate>Thu, 25 Feb 2010 17:56:48 +0000</pubDate>
		<guid isPermaLink="false">http://w.holeso.me/?p=38#comment-23204</guid>
		<description>the indentation was lost in the paste. here it is:

http://gist.github.com/314819</description>
		<content:encoded><![CDATA[<p>the indentation was lost in the paste. here it is:</p>
<p><a href="http://gist.github.com/314819" rel="nofollow">http://gist.github.com/314819</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Paul</title>
		<link>http://w.holeso.me/2008/08/a-simple-django-truncate-filter/#comment-23203</link>
		<dc:creator>Paul</dc:creator>
		<pubDate>Thu, 25 Feb 2010 17:53:53 +0000</pubDate>
		<guid isPermaLink="false">http://w.holeso.me/?p=38#comment-23203</guid>
		<description>Thanks that&#039;s useful but I found two bugs with this:
- it throws an error if there&#039;s no space in the string before max_length
- the get the character right after truncation, you should use value[max_length], not value[max_length + 1] which also solves the issue chris reported. 
Heres the fixed version:


from django import template
register = template.Library()

@register.filter(&quot;truncate_chars&quot;) 
def truncate_chars(value, max_length):
    if len(value) &lt;= max_length:
        return value

    truncd_val = value[:max_length]                        
    if value[max_length] != &quot; &quot;:
        rightmost_space = truncd_val.rfind(&quot; &quot;)
        if rightmost_space != -1:
            truncd_val = truncd_val[:rightmost_space]           

    return  truncd_val + &quot;...&quot;</description>
		<content:encoded><![CDATA[<p>Thanks that&#8217;s useful but I found two bugs with this:<br />
- it throws an error if there&#8217;s no space in the string before max_length<br />
- the get the character right after truncation, you should use value[max_length], not value[max_length + 1] which also solves the issue chris reported.<br />
Heres the fixed version:</p>
<p>from django import template<br />
register = template.Library()</p>
<p>@register.filter(&#8220;truncate_chars&#8221;)<br />
def truncate_chars(value, max_length):<br />
    if len(value) &lt;= max_length:<br />
        return value</p>
<p>    truncd_val = value[:max_length]<br />
    if value[max_length] != &#8221; &#8220;:<br />
        rightmost_space = truncd_val.rfind(&#8221; &#8220;)<br />
        if rightmost_space != -1:<br />
            truncd_val = truncd_val[:rightmost_space]           </p>
<p>    return  truncd_val + &#8220;&#8230;&#8221;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Neum</title>
		<link>http://w.holeso.me/2008/08/a-simple-django-truncate-filter/#comment-23188</link>
		<dc:creator>Neum</dc:creator>
		<pubDate>Tue, 09 Feb 2010 21:08:21 +0000</pubDate>
		<guid isPermaLink="false">http://w.holeso.me/?p=38#comment-23188</guid>
		<description>Excellent! That was exactly what I was looking for. I agree, this should be in Django proper. Until then, you saved me 10 min. :)  Thanks!</description>
		<content:encoded><![CDATA[<p>Excellent! That was exactly what I was looking for. I agree, this should be in Django proper. Until then, you saved me 10 min. <img src='http://w.holeso.me/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />   Thanks!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Chris Robinson</title>
		<link>http://w.holeso.me/2008/08/a-simple-django-truncate-filter/#comment-22456</link>
		<dc:creator>Chris Robinson</dc:creator>
		<pubDate>Wed, 07 Oct 2009 00:53:13 +0000</pubDate>
		<guid isPermaLink="false">http://w.holeso.me/?p=38#comment-22456</guid>
		<description>Worked beautifully thanks! No idea why truncating by characters isn&#039;t part of the default set of django filters</description>
		<content:encoded><![CDATA[<p>Worked beautifully thanks! No idea why truncating by characters isn&#8217;t part of the default set of django filters</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Daniel Karlsson</title>
		<link>http://w.holeso.me/2008/08/a-simple-django-truncate-filter/#comment-22192</link>
		<dc:creator>Daniel Karlsson</dc:creator>
		<pubDate>Tue, 19 May 2009 20:25:04 +0000</pubDate>
		<guid isPermaLink="false">http://w.holeso.me/?p=38#comment-22192</guid>
		<description>my django expert informed me that strings are lists and therefore we can use {{ some_list&#124;slice:&quot;:20&quot; }} or more specific {{ some_string &#124; slice:&quot;:20&quot;}}</description>
		<content:encoded><![CDATA[<p>my django expert informed me that strings are lists and therefore we can use {{ some_list|slice:&#8221;:20&#8243; }} or more specific {{ some_string | slice:&#8221;:20&#8243;}}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Graham King</title>
		<link>http://w.holeso.me/2008/08/a-simple-django-truncate-filter/#comment-21663</link>
		<dc:creator>Graham King</dc:creator>
		<pubDate>Fri, 01 May 2009 22:36:46 +0000</pubDate>
		<guid isPermaLink="false">http://w.holeso.me/?p=38#comment-21663</guid>
		<description>Exactly what I needed. Thank you.</description>
		<content:encoded><![CDATA[<p>Exactly what I needed. Thank you.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Chris</title>
		<link>http://w.holeso.me/2008/08/a-simple-django-truncate-filter/#comment-5067</link>
		<dc:creator>Chris</dc:creator>
		<pubDate>Mon, 02 Feb 2009 22:25:01 +0000</pubDate>
		<guid isPermaLink="false">http://w.holeso.me/?p=38#comment-5067</guid>
		<description>Minor error at a boundary condition. You can fix it by changing line 8 to:

if not len(value) == max_length+1 and value[max_length+1] != &quot; &quot;:</description>
		<content:encoded><![CDATA[<p>Minor error at a boundary condition. You can fix it by changing line 8 to:</p>
<p>if not len(value) == max_length+1 and value[max_length+1] != &#8221; &#8220;:</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: SmileyChris</title>
		<link>http://w.holeso.me/2008/08/a-simple-django-truncate-filter/#comment-12</link>
		<dc:creator>SmileyChris</dc:creator>
		<pubDate>Sun, 10 Aug 2008 23:40:06 +0000</pubDate>
		<guid isPermaLink="false">http://w.holeso.me/?p=38#comment-12</guid>
		<description>So kinda like http://code.djangoproject.com/ticket/5025 ? :)

Interesting idea about cutting it back to the last word, but if you&#039;re that worried about truncating characters, I&#039;d suggest it just inserts the ellipsis after truncating to length-3.</description>
		<content:encoded><![CDATA[<p>So kinda like <a href="http://code.djangoproject.com/ticket/5025" rel="nofollow">http://code.djangoproject.com/ticket/5025</a> ? <img src='http://w.holeso.me/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Interesting idea about cutting it back to the last word, but if you&#8217;re that worried about truncating characters, I&#8217;d suggest it just inserts the ellipsis after truncating to length-3.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

