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
|  
Report Content as Inappropriate

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()
    cur.execute(sql)
    data = cur.fetchall()
    cur.close()
    conn.close()
    return data

import cherrypy

class app():
    @cherrypy.expose
    def index(self):
        header ="""
        <!DOCTYPE html>
        <html>
          <head>
            <meta charset="utf-8">
            <title></title>
          </head>
          <body>
            <h1>user list:</h1>
        """
        footer ="""
          </body>
        </html>

        """
        list = []
        list.append(header)
        data = select('select * from users')

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

        list.append(footer)
        return ''.join(list)

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


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

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

    @cherrypy.expose
    def displayImages(self, *args, **kwargs):
        db,a=getDb()
        try:
            sql="Select * from products;"
            a.execute(sql)

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

        a.close()
        db.close()

How do I show the results in cherry template?

Thanks

Arjuna

--
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 https://groups.google.com/group/cherrypy-users.
For more options, visit https://groups.google.com/d/optout.
Loading...