In a Django model passing a param to a method and using it in the code is easy.
Class Foo(models.Model):
number = IntegerField()
...
def bar(self, percent):
return self.number * percent
f = Foo(number=250)
f.bar(10)
The question is how can this be done in the template layer? Somthing like :
{{ foo.bar(10) }}
The simple answer is that you can't do this, which is by design; Django templates are designed to be keep you from writing real code in them. Instead, you'd have to write a custom filter, e.g.
@register.filter
def bar(foo, percent):
return foo.bar( float(percent) )
This would let you make a call like {{ foo|bar:"250" }} which would be functionally identical to your (non-working example) of {{ foo.bar(250) }}.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With