Reputation: 25
I have this line of code in my Django templates that returns counts of comments like 1, 2, 3...
<li class="list-inline-item">comms: {{ article.comments.count }}</li>
How do I make it return counts in twos for every comment? like 2, 4, 6...
I can provide further details if need be.
Thanks.
Upvotes: 0
Views: 37
Reputation: 2474
Multiple solutions for this:
Article
model which will do the multiplication for youclass Article(models.Model):
...
@property
def comments_count_multiplied(self):
return 2 * self.comments.count()
Now you can use this in your template:
<li class="list-inline-item">comms: {{ article.comments_count_multiplied }}</li>
from django import template
register = template.Library()
@register.filter
def multiply_with_two(value):
return 2 * value
And in your template:
<li class="list-inline-item">comms: {{ article.comments.count|multiply_with_two }}</li>
Upvotes: 2