dhana dhana - 13 days ago 5
Python Question

How to use generator method in django template?

I want to use python generators in django template, Is it possible?

For example, I have model and it contains generator object as shown bellow,

class TestMe(models.Model):
@property
def gen(self):
yield 1
yield 2


context['gen'] = gen

then in template,

{{ gen }} # it should be print 1
{{ gen }} # it should be print 2


Without using for loop

I have tried this way but it returns the python generator not 1. Any one have idea about this.

Answer

In template

{{ gen.next }}
{{ gen.next }}
Comments