我有 2 个处理程序。一个称为 MainHandler,它呈现一个小表单来注册用户(创建帐户)。提交电子邮件和密码后,MainHandler 会检查帐户不存在,验证字段,然后创建一个新的用户实体。然后重定向到 HomeHandler (/home) 并将用户电子邮件作为 URL 查询参数发送,即“http://localhost:8000/home?email=jack@smith.com”
我的问题是,这是最好的方法吗?在 HomeHandler 有另一种形式,允许用户输入一个地址,该地址将是用户的孩子。使用电子邮件,我运行查询以查找用户。如果我不通过用户电子邮件发送 HomeHandler 如何知道哪个用户正在输入地址?如果我有其他处理程序将接收要存储并与用户关联的其他数据,我是否必须每次都继续发送用户电子邮件?似乎应该有更好的方法来做到这一点,但我无法弄清楚。
class User(db.Model):
email = db.EmailProperty()
password = db.StringProperty()
class Address(db.Model):
line1 = db.StringProperty()
line2 = db.StringProperty()
class MainHandler(webapp2.RequestHandler):
def get(self):
renders a template with a form requesting email and pwd
def post(self):
Validates form and checks account doesn't already exist
if (user doesn't already exist and both email and pwd valid):
newuser = User(email=email, password=password);
newuser.put();
self.redirect("/home?email=%s"%email)
class HomeHandler(webapp2.RequestHandler):
def get(self):
Renders another form requesting a physical address (2 lines)
def post(self):
email=self.request.get("email")
addressLine1 = self.request.get("address1")
addressLine2 = self.request.get("address2")
q = db.Query(User).filter('email =', email)#Construct query
userMatchResults = q.fetch(limit=1)#Run query
homeAddress = Address(parent=userMatchResults[0])
homeAddress.line1 = addressLine1
homeAddress.line2 = addressLine2
homeAddress.put()
app = webapp2.WSGIApplication([('/', MainHandler), ('/home', HomeHandler)], debug=True)