djangotemplates

How to pass an argument to a method on a template variable in Django?


I am using the lxml library to define a variable (category) in a view. lxml provides a method .get to retrieve custom attributes. I'd like to use it in the template like so:

{{ category.get("foo") }} 

I know that when using template variables it is unnecessary to use parenthesis, but I get the following error:

{{ category.get "foo" }}

Could not parse the remainder: ' "foo"' from 'category.get "foo"'

I'm assuming that there is something wrong with my syntax but google has been no help. The django docs say that methods are looked up by using a .


Solution

  • You can't pass an argument to a callable attribute like this. Either pull the value in the view, or write a custom template tag to do it.