Reputation: 321
I have 2 Handlers. One is called MainHandler, which renders a small form to sign up a user(create an account). Upon submitting email and pwd, MainHandler checks the account doesn't exist already, validates the fields, and then creates a new User entity. Then redirects to HomeHandler (/home) and sends the user email as a URL query parameter, i.e. "http://localhost:8000/[email protected]"
My question is, is that the best way to do it?? At HomeHandler there's another form that allows the user to enter an address that will be a child of the user. Using the email, I run a query to find the user. If I don't send over the user email how will HomeHandler know which user is entering the address? If I have other handlers that will receive other data to be stored and associated with the user, do I have to keep sending the user email every time? Seems like there should be a better way to do it, but I can't figure it out.
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)
Upvotes: 0
Views: 49
Reputation: 11706
You do not have to redirect. You can send the second form in the post of Mainhandler. And you can combine both handlers, If the post of the main handler can detect if the post request origin is the first or the second form. An easy way to do this, is adding a hidden input field to both forms, with the name of the form. This field will be part of the post data.
But there are many other ways to preserve the state between requests.
Upvotes: 1