Re: Showing records from DB in cherry template

Previous Topic Next Topic
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view

Re: Showing records from DB in cherry template

Stephan Semerad

To show records from a db, I do the following with PosgreSQL
I use a select function and then reiterate it into a list, join the list, and then return the values. 
but you can make it more fancy.

import psycopg2

ip = ''
database = ''
user = ''
password = ''

def select(sql):
    conn=psycopg2.connect(host=ip,database=database, user=user, password=password,port=5432)
    cur = conn.cursor()
    data = cur.fetchall()
    return data

import cherrypy

class app():
    def index(self):
        header ="""
        <!DOCTYPE html>
            <meta charset="utf-8">
            <h1>user list:</h1>
        footer ="""

        list = []
        data = select('select * from users')

        for i in data:
            list.append("<h1>usernames: %s</h1>" % (i[1]) )

        return ''.join(list)

cherrypy.quickstart(app(), "/")

On Saturday, June 3, 2006 at 10:39:45 AM UTC+2, brahmaforces wrote:

I wanted to find out how to show a straight list of records from a db
using Cherry Template. I am trying to write a module that shows an
image of a product, a description, price etc. Is there any code that
exists that uses python and cherrytemplate in cherrypy to do this. My
code thus far is

    def displayImages(self, *args, **kwargs):
            sql="Select * from products;"

        except MySQLdb.Error, e:
            print "Error %d: %s" % (e.args[0], e.args[1])
            sys.exit (1)


How do I show the results in cherry template?



You received this message because you are subscribed to the Google Groups "cherrypy-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at
For more options, visit