python函数变量无效怎么办_python seek函数在Python的Django框架中调用方法和处理无效变量...

方法调用行为

方法调用比其他类型的查找略为复杂一点。 以下是一些注意事项:

在方法查找过程中,如果某方法抛出一个异常,除非该异常有一个 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()方法将不会被执行。 它将静静地错误退出。

如何处理无效变量

W WW.002PC .COM对《在Python的Django框架中调用方法和处理无效变量》总结来说,为我们网站制作很实用。

默认情况下,如果一个变量不存在,模板系统会把它展示为空字符串,不做任何事情来表示失败。 例如:

>>> from django.template import Template, Context

>>> t = Template('Your name is {{ name }}.')

>>> t.render(Context())

u'Your name is .'

>>> t.render(Context({'var': 'hello'}))

u'Your name is .'

>>> t.render(Context({'NAME': 'hello'}))

u'Your name is .'

>>> t.render(Context({'Name': 'hello'}))

u'Your name is .'

系统静悄悄地表示失败,而不是引发一个异常,因为这通常是人为错误造成的。 这种情况下,因为变量名有错误的状况或名称, 所有的查询都会失败。 现实世界中,对于一个web站点来说,如果仅仅因为一个小的模板语法错误而造成无法访问,这是不可接受的。

更多:python seek函数在Python的Django框架中调用方法和处理无效变量

https://www.002pc.comhttps://www.002pc.com/python/725.html

你可能感兴趣的Python,Django,调用,无效,变量,框架

No alive nodes found in your cluster

0踩

0 赞

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值