如何传递参数到Django中的模板变量的方法? [英] How to pass an argument to a method on a template variable in Django?

查看:686
本文介绍了如何传递参数到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.

这篇关于如何传递参数到Django中的模板变量的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆