Custom Search

Tuesday, March 30, 2010

How add 'read more...' link using django template filter truncatewords

How add 'read more...' link using django template filter truncatewords

I want to add a read more link if description contain more than 20 words
I am using filter truncatewords:20
I also want to hide read more... link , if words less than 20

solution:

{% ifnotequal foo foo|truncatewords:20 %}
<a href="...">Read more</a>
{% endifnoteuqal %}

The advantage this solution has is it works with truncatewords_html as well.

Example:

<p>
{{ note.description|linebreaks|safe|truncatewords:20 }}
{% ifnotequal note.description note.description|truncatewords:20 %}
<a class="readmore" href="{% url note_detail project.id note.id %}">read more...</a>
{%endifnotequal%}
</p>


------------

You can use the wordcount filter instead

-----------

3 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. Hi nice work! I want to learn how can i add comment section to my blog project. Can you blog about this?

    ReplyDelete