2

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 %}
1
  • Show us the code where you render the template. If Rostyslav's answer isn't helping, it's probably a problem with the template context. Commented Aug 9, 2012 at 15:15

1 Answer 1

5

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

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, Rostyslav Dzinko. I corrected that, but the error persistis. Nothing (in the for loop) is shown. Other template parts are ok.
Let's advance. Jinja2 is a template engine and those template is not a python code, so you must obey its rules. profession = person.profession won't be processed like python code (assignment), but jinja2 syntax allows you to access object attributes, so you can render attributes like {{ party.nacionality }}.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.