Hi,
I'm new to python and I'm trying to obtain a username from the UI and query the result to get the phone book contacts of the user. But I am not able to set the user name to be a global variable to use it for multiple queries. Here's the code, I believe I am doing some syntax error/improper usage, please help out in correcting my code.
#!/usr/bin/env python
import wsgiref.handlers
from google.appengine.ext import db
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
import models
class showPhoneBook(db.Model):
username = db.StringProperty(required=True)
class MyHandler(webapp.RequestHandler):
user_name = ''
def get(self):
p = db.GqlQuery('SELECT * FROM UserDetails WHERE user_name = $1', user_name)
#p = UserDetails.gql('WHERE user_name = $1', user_name)
result1 = p.fetch(1)
for itr1 in result1:
userId = itr.user_id
q = db.GqlQuery('SELECT * FROM PhoneBook WHERE user_id = :1', userId)
#q = PhoneBook.gql('WHERE user_id = :1', userId)
values = {
'phoneBookValues': q
}
self.request.out.write(
template.render('phonebook.html', values))
def post(self):
global user_name
phoneBookuserID = showPhoneBook(
user_name = self.request.get(
'username'))
self.request.out.write(user_name)
phonebookuserID.put()
self.redirect('/')
def main():
app = webapp.WSGIApplication([
(r'.*',MyHandler)], debug=True)
wsgiref.handlers.CGIHandler().run(app)
if __name__ == "__main__":
main()
Here UserDetails and Phonebook are my entity classes defined in my models.py
Please Help me in finding the error...or possibly get a corrected code of how to use user_name in queries after obtaining it from the UI.