【发布时间】:2015-11-10 14:43:06
【问题描述】:
我想在django模板中使用python生成器,可以吗?
例如,我有模型,它包含如下所示的生成器对象,
class TestMe(models.Model):
@property
def gen(self):
yield 1
yield 2
上下文['gen'] = gen
然后在模板中,
{{ gen }} # it should be print 1
{{ gen }} # it should be print 2
不使用for循环
我已经尝试过这种方式,但它返回的 python 生成器不是 1。任何人都知道这一点。
【问题讨论】: