Le 20/07/2012 18:38, Arnaud Vandecasteele a écrit :
/
cur.execute(""" select osm_user from osm_user_statistics """)
sql_users = cur.fetchall()/
env = Environment(loader=PackageLoader('osm-stat', 'templates'))
template = env.get_template('stats.html')
result = template.render( users = sql_users)
The templates is like that :
<ul id="contributeurs">
{% for user in users %}
<li>{{ user.osm_user }}</li>
{% endfor %}
</ul>
The fact that I dont understand is the result display two entries (I've
got two users) but not the name of them :
<ul id="contributeurs">
<li></li>
<li></li>
</ul>
Could you explain me what i'm doing wrong ?
Hi Arnaud,
According to DBAPI:
http://www.python.org/dev/peps/pep-0249/
fetchall() returns a list of tuples. "user" in each iteration of your
Jinja loop is a tuple. Tuples do not have attributes, so user.osm_user
is Jinja’s Undefined object. The default behavior when printing
Undefined it to print the empty string.
Try this instead:
{{ user[0] }}
Alternatively, unpack the tuples in the for-loop:
{% for osm_user, in users %}
Notice the comma. (osm_user,) is a one-element tuple. The general syntax
is "for a, b, c in list_of_tuples".
If you use a higher-level library like SQLAlchemy or SQLSoup rather than
raw DBAPI, you can get nicer objects with named attributes rather than
bare tuples.
Regards,
--
Simon Sapin
--
You received this message because you are subscribed to the Google Groups
"pocoo-libs" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/pocoo-libs?hl=en.