Reputation: 6221
I'm building a Django blog app, since that's apparently what all the cool kids are doing. I've built a template tag to display only the beginning of the post if it's long, and ideally to include a link to the whole post. I started looking into escaping and IsSafe=True, but my worry is that the content itself could have HTML tags in it that could mess things up.
Here's what I have now:
@register.filter(name='shorten')
def shorten(content):
#will show up to the first 500 characters of the post content
if len(content) > 500:
return content[:500] + '...' + <a href="/entry/{{post.id}}">(cont.)</a>
else:
return content
Upvotes: 0
Views: 144
Reputation: 22808
from django.utils.safestring import mark_safe
@register.filter(name='shorten')
def shorten(content, post_id):
#will show up to the first 500 characters of the post content
if len(content) > 500:
output = "{0}... <a href='/entry/{1}'>(cont.)</a>".format(content[:500], post_id)
else:
output = "{0}".format(content)
return mark_safe(output)
Upvotes: 1