如何使用webapp2从表单中获取信息?

时间:2015-03-30 00:02:56

标签: python html webapp2

我有以下情况:

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

此表单位于.html文件中,显然位于与python代码不同的文件中。我希望知道我有哪些方法可以从表单中获取信息并发送到python文件以最终处理它(我猜是关于action字段但不确定)。

OBS:我必须使用webapp2(我使用谷歌服务器,所以django和其他东西不起作用)

1 个答案:

答案 0 :(得分:4)

您可以在Google App Engine wepapp2教程中看到Handling Forms with webapp2

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)

请仔细阅读整个教程,了解有关Datastoretemplate

的更多信息

使用template允许您将代码html代码放在另一个文件中。

相关问题