Reputation:
I am trying to pass 3 variables to my custom filter, but I can’t do it. Is there any such possibility in Django?
{{ item.cost|pay_blago:'{{item.can_pay_blago_to_currency}}, {{item.rest}}'}}
@register.filter
def pay_blago(cost, args):
print(args.split(','))
Upvotes: 0
Views: 466
Reputation: 8192
If you are needing somethinf that's a function of an Item
instance, the easiest way is a property defined on Item
. In the Template, say,
{{item.cost_pay_blago}}
and on the Item
model
# this is needed by template foo/bar.html
@property
def cost_pay_blago( self):
result = # some value based on self.cost, self.rest and self.can_pay_blago_to_currency
return result
if item
is not a model instance or if the function brings together data from multiple objects, you probably compute it in the view and put it into the context that you render the template against.
Or you could use the Jinja template engine, but that's a very big hammer to use if it's a small nut.
Upvotes: 0
Reputation: 212
Django only allows one parameter. You can send it as a list/tuple and split it inside the filter.
There is a ticket for that too with (wontfix flag) https://code.djangoproject.com/ticket/1199
Upvotes: 1