【问题标题】:How do I get information from a form using webapp2?如何使用 webapp2 从表单中获取信息?
【发布时间】:2015-06-02 21:27:46
【问题描述】:

我有以下场景:

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

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

OBS:我必须使用 webapp2(我使用的是 google 服务器,所以 django 和其他东西在它上面不起作用)

【问题讨论】:

    标签: python html webapp2


    【解决方案1】:

    您可以在 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 代码放在另一个文件中。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-07-15
    • 1970-01-01
    • 2023-01-19
    • 2022-01-10
    • 1970-01-01
    • 2015-04-17
    • 2013-02-23
    相关资源
    最近更新 更多