我读到 capitalize() 方法用于将字符串的第一个字母从小写更改为大写。 我正在用 Django 编写一个小型网站(第一次),我注意到在模板部分,如果我编写 item.description.capitalize() 会出现一些错误,但是删除括号后它会起作用,为什么?
capitalize不是一个方法吗??
请您参考如下方法:
Isn't capitalize a method?
是。但是在 Django 模板中,如果您编写 {{ foo }},并且 foo 是一个 callable,Django 会自动调用该 callable。 {{ foo.bar }} 相同:如果 foo 是可调用的,则在尝试访问 .bar 之前首先调用它,和 ['bar']。如果该结果是可调用的,则再次调用它。这在 documentation on template variables [Django-doc] 中指定:
If a variable resolves to a callable, the template system will call it with no arguments and use its result instead of the callable.
Django 的模板有意限制不允许下标、函数调用等。因为通常这意味着您在模板中编写业务逻辑。
