Hi Simon, Thank you for your help. You were right the fetchall return a tuples. As you told me I can use user[0] But I found an extra method for psycopg : cur = conn.cursor(cursor_factory=psycopg2.extras.DictCursor) Now the tuples are transform into a dictionary
Thank you again for your help Arnaud On Fri, Jul 20, 2012 at 7:51 PM, Simon Sapin <[email protected]> wrote: > 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/<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 pocoo-libs+unsubscribe@** > googlegroups.com <pocoo-libs%[email protected]>. > For more options, visit this group at http://groups.google.com/** > group/pocoo-libs?hl=en <http://groups.google.com/group/pocoo-libs?hl=en>. > > -- -------------------------------------------------------------------- Arnaud Van De Casteele Mines Paris Tech - CRC Sophia-Antipolis 0698 24 25 29 SIG - WebMapping - Spatial Ontology - GeoCollaboration Web Site http://perso.crc.mines-paristech.fr/~arnaud.van_de_casteele/ http://geotribu.net/ http://www.i2c.eu/ -- 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.
