dhana dhana - 1 year ago 97
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):
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 Source

In template

{{ gen.next }}
{{ gen.next }}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download