Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get information from a form using webapp2?

I have the following scenario:

<form class=*** method="post" action=("main.py" ???)>
<input class=*** type="email" name="email">
<input class=*** type="submit" value=***>
</form>

This form is in a .html file, evidently in a different file from the python code. I wish know which ways do I have to get the information from the form and send to the python file to finally work on it (I guess is something about the action field but not sure).

OBS: I must use the webapp2 (I'm using the google server so django and other stuff do not work on it)

like image 933
Vítor Lourenço Avatar asked May 20 '26 09:05

Vítor Lourenço


1 Answers

You can see Handling Forms with webapp2 from the Google App Engine wepapp2 tutorial.

import cgi
from google.appengine.api import users
import webapp2

MAIN_PAGE_HTML = """\
<html>
  <body>
    <form action="/sign" method="post">
      <div><textarea name="content" rows="3" cols="60"></textarea></div>
      <div><input type="submit" value="Sign Guestbook"></div>
    </form>
  </body>
</html>
"""

class MainPage(webapp2.RequestHandler):
    def get(self):
        self.response.write(MAIN_PAGE_HTML)

class Guestbook(webapp2.RequestHandler):
    def post(self):
        self.response.write('<html><body>You wrote:<pre>')
        self.response.write(cgi.escape(self.request.get('content')))
        self.response.write('</pre></body></html>')

application = webapp2.WSGIApplication([
    ('/', MainPage),
    ('/sign', Guestbook),
], debug=True)

And read through the entire tutorial for more information about Datastore and template

Using template allow you to put code html code in another file.

like image 145
Aaron Avatar answered May 22 '26 21:05

Aaron