[Answered ]-Prevent multiple hits to database in django templates

2👍

You can use the with tag

{% with task_count=user.get_num_tasks %}
    <p {% if task_count > 0 %} class="green" {% endif %}>{{ task_count }}</p>
{% endwith %}

Edit: I had to remove the spaces before and after
the assignment (task_count = user.get_num_tasks), otherwise it crashes.

Leave a comment