我想在Django模板中使用python生成器,這可能嗎?如何在django模板中使用生成器方法?
例如,我有模型,它包含如圖波紋管發電機對象,
class TestMe(models.Model):
@property
def gen(self):
yield 1
yield 2
上下文[ '根'] =根
然後在模板,
{{ gen }} # it should be print 1
{{ gen }} # it should be print 2
在不使用for循環
我已經試過這種方式,但它返回的python生成器不是1。任何人都有這個想法。
好的答案!不幸的是,這在Python 3中不起作用,您可以在其中調用'next(gen)'而不是'gen.next()'。 – Alasdair