[Django]-Django – use template tag and 'with'?

31👍

You could turn it into a filter:

{% with user|uploads_for as upload_count %}

14👍

While a filter would still work, the current answer to this question would be to use assignment tags, introduced in Django 1.4.

So the solution would be very similar to your original attempt:

{% uploads_for_user leader as upload_count %}
{{ upload_count }} upload{{ upload_count|pluralize }}

Update: As per the docs assignment tags are deprecated since Django 1.9 (simple_tag can now store results in a template variable and should be used instead)

👤imiric

11👍

In Django 1.9 django.template.Library.assignment_tag() is depricated:
simple_tag can now store results in a template variable and should be used instead.

So, now simple tag we can use like a:

It’s possible to store the tag results in a template variable rather
than directly outputting it. This is done by using the as argument
followed by the variable name. Doing so enables you to output the
content yourself where you see fit:

{% get_current_time "%Y-%m-%d %I:%M %p" as the_time %}
<p>The time is {{ the_time }}.</p>

Leave a comment