4đź‘Ť
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) }}
.
1đź‘Ť
By design, Django templates don’t allow you to invoke methods directly so you’d have to create a custom template tag to do what you want.
Jinja2 templates do allow you to invoke methods, so you could look into integrating Jinja2 with Django as another option.
- [Django]-Django ValueError: 'dtedeclared with a lazy reference to …..' when changed Model Name
- [Django]-Django Aggregation for Goals
- [Django]-Error with pip install MySQL-python
0đź‘Ť
Just do this calculation in the view, and not the template.
This is often the solution to many “template language can’t do X” problems.
view
foo = Foo(number=250)
foo.bar = foo.bar(10)
return direct_to_template(request, "foo.html", {'foo': foo})
template
{{ foo.bar }}