如何传递参数到Django中的模板变量的方法?
我正在使用lxml库在视图中定义一个变量(category)。 lxml提供了一个方法 .get
来检索自定义属性。我想在模板中使用它:
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") }}
我知道当使用模板变量时,不必使用括号,但是我收到以下错误: / p>
I know that when using template variables it is unnecessary to use parenthesis, but I get the following error:
{{ category.get "foo" }}
无法解析剩余部分:'foo'from'category.getfoo'
Could not parse the remainder: ' "foo"' from 'category.get "foo"'
我假设我的语法有问题,但google一直没有帮助。 django docs说通过使用来查找方法。
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 .
您不能将参数传递给这样的可调用属性。在视图中拉取值,或者写入自定义模板标签来执行。
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.