craftApprentice
craftApprentice

Reputation: 2777

How to properly call and use variables in jinja2 template with embedded Python code?

I have this two classes:

class Person(db.Model):
    person_name = db.StringProperty(required = True)
    #gender = db.StringProperty(required = True)
    nacionality = db.StringProperty(required = True)
    marital_status = db.StringProperty(required = True)
    profession = db.StringProperty(required = True)
    SSN = db.IntegerProperty(required = True)
    driver_license = db.IntegerProperty(required = True)
    address = db.PostalAddressProperty(required = True)

class ContractingParty(db.Model):
    person = db.ReferenceProperty(Person, required=True, collection_name="party_to_contracts")
    contract = db.ReferenceProperty(Contract, required=True)
    condition = db.StringProperty()

I want to pass a query of ContractingParty entities to my jinja2 template. Then with a for loop, I want to acess the data I really want, from Person entities. The Contracting Party query is being passed to jinja2 (if I test it, I can see something like this: [<main.ContractingParty object at 0x0492D770>]). But the for loop bellow doesn't working, no information of the for loop is being shown in my brownser. How could I fix it?

{% for party in parties %}
     <li> {{party.person.person_name}} </li>
{% endfor %}

Upvotes: 2

Views: 2182

Answers (1)

Rostyslav Dzinko
Rostyslav Dzinko

Reputation: 40755

Seems like you've made a mistake in your for loop construct, colon character must be omitted:

{% for party in parties %}

The whole code must be changed to actually render objects passed to templates. You can't execute Python code here, you must obey Jinja2 syntax:

{% for party in parties %}
    <li>{{ party.person.profession}}</li>
    ...
{% endfor %}

If you want to make some assignment use set Jinja's tag:

{% set person = party.person %}

I hope you've got the idea, here is a link with much more clarification

Upvotes: 5

Related Questions