PYTHON和PSOTGRESQL分页,怎么这么难呀..

PYTHON和PSOTGRESQL分页,怎么这么难呀..

2个文件:index.cgi   fh.py  只能显示第一页,跳到第2页就是空白页了,,晕死了,,,,,,,,,我菜死了....
谁会呀,,,会的来指点指点呀........晕死了....
index.cgi:                                                                  
#!/usr/bin/python
import fh
if (__name__=="__main__"):
    fh.printHeader("hello python")
    fh.htmlshow()

fh.py
#!/usr/bin/python
#!-*- coding:utf-8 -*-
import string,sys,os
import psycopg,cgi,time
def printHeader(title):
    print"""Content-type:text/html
    <?xml version="1.0" encoding="utf-8"?>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
      "DTD/xhtml1-strict.dtd"
    >
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head><title>%s</title></head>
    <body>"""%title
   
def htmlshow():
    global page,allpg
    pagesize=5
    page=1
   
    dbname="wxb"
    tablename="alldata"
    usrname="go"
    dbcon=psycopg.connect("dbname=%s "%(dbname))
    if(dbcon==None):
        print"(Error)can not connect to the db[%s]"%(dbname)
        sys.exit()
    cur=dbcon.cursor()
   
    cmd2="select count(*) from alldata;"

    cur.execute(cmd2)
    rows=cur.fetchone()
    rowsnum=rows[0]
    if(rowsnum%5==0):
        allpg=rowsnum/5
    else:
        allpg=rowsnum/5+1
    form=cgi.FieldStorage()
    if form.has_key("page")
        page=form["page"].value
    else:
        page=1
    offset=pagesize*(page-1)
    cmd="select * from alldata limit %d offset %d;"%(pagesize,offset)
    cur.execute(cmd)
    rec=cur.fetchall()
   
    print """
    <TABLE BORDER=1 cellspacing=0 cellpadding=3 WIDTH=95%>
    <tr>
    <table=100%>
    <TR bgcolor=green>
    <TD colspan=2>
    <center>
    <h1>Welcome to Python </h1>
    </center>
    </TD>
    </TR></table></tr>"""
   
    if(pg_fetch_array(rec)):
        print"<p><span>"
        print "%7d: name:<font size=4 color=blue>%s |</font>\
        email:<font size=4 color=blue>%s |</font><font size=4 color=blue>%s</font>"%(rec[0],rec[1],rec[2],rec[3])
        print"</span></p>"
        print"<p>"
        print"<font size=3 color=black>%s</font>"%(rec[4])
        print"</p>"
    print"</table></tr>"
    print"""<form method="get" action="fh.py">"""
    first=1                prev=page-1                          next=page+1                              last=allpg
    if(page>1 and page<allpg):
        print """<a onclick="homepage()" href='index.cgi?page=%d'>"homepage"</a>
        <a onclick="prev()" href='index.cgi?page=%d'>"prev"</a>
        <a onclick="next()" href='index.cgi?page=%d'>"next"</a>
        <a onclick="last()" href='index.cgi?page=%d'>"last"</a>"""%(first,prev,next,last)
    if(page==allpg):
        print """<a onclick="homepage()" href='index.cgi?page=%d'>"homepage"</a>
        <a onclick="prev()" href='index.cgi?page=%d'>"prev"</a>
        <a onclick="next()" href='index.cgi?page=%d'>"next"</a>"""%(first,prev,next)
    else:
        print """<a onclick="next()" href='index.cgi?page=%d'>"next"</a>
        <a onclick="last()" href='index.cgi?page=%d'>"last"</a>"""%(next,last)
           
    print"""<input type="hiddn" name="page" value=%d/>"""%(page)
    print"""\
    <p>
    username:<input type="text" name="user_name" size="30"/>
    email:<input type="text" name="email" size="30"/>
    </p>
    <p>
    <textarea name="t_txt" cols=80 rows=8"></textarea>
    </p><input type="submit" value="send"/>
    </form>"""
    dbin()
    print"""</body></html>"""

def homepage():
    global page
    page=1
    return page

def prev():
    global page
    page=page-1
    return page

def next():
    global page
    page=page+1
    return page

def last():
    global page,allpg
    page=allpg
    return page

def dbin():  这个是用来插入数据的,没问题..............

连标题都拼错,很难看懂啊
postgresql
page,allpg全局变量在哪儿定义的?
<a onclick="next()" href='index.cgi?page=%d'>"next"</a>
传给index.cgi一个page值,没看到你怎么用
sqlalchemy