python - <type 'exceptions.NameError'> -
trying create web application (using mysql , python) list of hiking trails in ma. want display names of trails in db on 1 page , cannot figure out why nothing display:
################################################################################ def getallhikes(): """ middleware function read database. returns list containing records of trails have in table. """ # connect db conn, cursor = getconnectionandcursor() # prepare sql sql = """ select name hiking """ # run sql cursor.execute(sql) # fetch results data = cursor.fetchall() # clean cursor.close() conn.close() return data ################################################################################ def showallhikes(data): '''produce table showing trails, 1 per row.''' print(''' here popular trails have on file: <table> <tr> <td>%s</td> <td>%s</td> <td>%s</td> <td>%s</td> <td>%s</td> </tr> ''') % (name, town) print(''' </table> ''') ################################################################################ if __name__ == "__main__": # form field data form = cgi.fieldstorage() dohtmlhead("masshike: database of popular massachusetts trails") data = getallhikes() showallhikes(data) dohtmltail()
i encounter problem when try work web applications. error saying args = ("global name 'name' not defined",)
if can explain in simple terms appreciate it. don't understand @ all. thanks!
the issue within method below. not have name
or town
defined within function or @ global
level.
def showallhikes(data): '''produce table showing trails, 1 per row.''' print(''' here popular trails have on file: <table> <tr> <td>%s</td> <td>%s</td> <td>%s</td> <td>%s</td> <td>%s</td> </tr> ''') % (name, town) print(''' </table> ''')
in order information need go through data
according mysql fetchall appears list
of tuple
.
with information can following:
print(''' here popular trails have on file: <table>''') name, town in data: print(''' <tr> <td>%s</td> <td>%s</td> </tr> ''') % (name, town) print(''' </table> ''')
however, looks sql query needs include town information sql = """select name hiking"""
, more fail with:
traceback (most recent call last): file "<console>", line 1, in <module> valueerror: need more 1 value unpack
Comments
Post a Comment