方法调用行为
视频锁定
{$ currentTime | date:'mm:ss' $}
{$ timeLeft | date:'mm:ss' $}
方法调用比其他类型的查找略为复杂一点。 以下是一些注意事项:
在方法查找过程中,如果某方法抛出一个异常,除非该异常有一个
silent_variable_failure
属性并且值为True
,否则的话它将被传播。如果异常被传播,模板里的指定变量会被置为空字符串,比如:
>>> t = Template("My name is {{ person.first_name }}.")
>>> class PersonClass3:
... def first_name(self):
... raise AssertionError, "foo"
>>> p = PersonClass3()
>>> t.render(Context({"person": p}))
Traceback (most recent call last):
...
AssertionError: foo
>>> class SilentAssertionError(AssertionError):
... silent_variable_failure = True
>>> class PersonClass4:
... def first_name(self):
... raise SilentAssertionError
>>> p = PersonClass4()
>>> t.render(Context({"person": p}))
u'My name is .'
仅在方法无需传入参数时,其调用才有效。 否则,系统将会转移到下一个查找类型(列表索引查找)。
显然,有些方法是有副作用的,好的情况下允许模板系统访问它们可能只是干件蠢事,坏的情况下甚至会引发安全漏洞。
例如,你的一个
BankAccount
对象有一个delete()
方法。 如果某个模板中包含了像{{ account.delete }}
这样的标签,其中account
又是BankAccount
的一个实例,请注意在这个模板载入时,account
对象将被删除。要防止这样的事情发生,必须设置该方法的
alters_data
函数属性:
def delete(self):
# Delete the account
delete.alters_data = True
- 模板系统不会执行任何以该方式进行标记的方法。 接上面的例子,如果模板文件里包含了
{{ account.delete }}
,对象又具有delete()
方法,而且delete()
有alters_data=True
这个属性,那么在模板载入时,delete()
方法将不会被执行。 它将静静地错误退出。
在线练习
{$ activeFileHint $}