【问题标题】:Getting display after form post submission in Google App Engine in Python在 Python 中的 Google App Engine 中提交表单后获取显示
【发布时间】:2011-03-22 12:31:07
【问题描述】:

表单发布后不显示页面内容,但直接查看页面时显示。我有一段 Python App Engine 代码试图指向一个新页面并显示一段以编程方式定义的(即在代码中,而不是在 html 中)文本。但是,按下表单的提交按钮,我得到一个空白页,没有错误消息。

我一直在使用 Google App 引擎代码示例。该表单只需要一些选项,但我什至没有从中收集任何内容,应该转到新页面,但它没有,我无法找出它可能出错的地方。

我有

class MainPage(webapp.RequestHandler):
    def get(self):

        path = os.path.join(os.path.dirname(__file__), 'index.html')
        self.response.out.write(template.render(path, template_values))

class Confirm(webapp.RequestHandler):
    def post(self):
       self.response.headers['Content-Type'] = 'text/plain'
       self.response.out.write('You have confirmed!')



application = webapp.WSGIApplication(
                                 [('/', MainPage),
                                  ('/confirm', Confirm)],
                                 debug=True)

def main():
   run_wsgi_app(application)

if __name__ == "__main__":
   main()

在 HTML 中:index.html

<html>
        <body>
          <form action="/confirm" method="post">
            <div><textarea name="content" rows="3" cols="60"></textarea></div>
            <div><input type="submit" value="Submit"></div>
          </form>
        </body>
      </html>

我想知道为什么如果我提交表单,我没有得到你已经确认!消息,但如果我去 /confirm 我会这样做。谢谢。

【问题讨论】:

  • 您的 MainPage 处理程序似乎有缩进问题。这是在这里编辑时缩进的问题,还是你有这样的问题?
  • 缩进问题只是在 S.O.
  • 为你解决了缩进问题

标签: python google-app-engine


【解决方案1】:

您的代码运行得非常顺利;可能你没有正确实现 post 方法来解决一些奇怪的缩进错误(这可以解释 405 错误)。

复制并粘贴我的application.py 并重试:

from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app

class MainPage(webapp.RequestHandler):
    def get(self):
        path = os.path.join(os.path.dirname(__file__), 'index.html')
        self.response.out.write(template.render(path, {}))

class Confirm(webapp.RequestHandler):
    def post(self):    
       self.response.headers['Content-Type'] = 'text/plain'
       self.response.out.write('You have confirmed!')

application = webapp.WSGIApplication(
                                 [('/', MainPage),
                                  ('/confirm', Confirm)],
                                 debug=True)

def main():
    run_wsgi_app(application)

if __name__ == "__main__":
    main()

【讨论】:

    【解决方案2】:

    我会测试您的断言,即使用 curl 发布内容的 POST to Confirm 正常工作:

    curl -v0 -F content=blah -F submit=submit 'http://my-app.appspot.com/confirm'

    如果一切正常,我会使用 HttpFox(FF 扩展)来查看发送到服务器的内容。

    似乎您没有提交您认为的内容,或者您​​的 POST 处理程序没有按照您的想法工作。上述两个步骤都应该有助于明确哪个是哪个。

    【讨论】:

    • 在命令行上查看 dev_server 的输出,每当提交页面时,我都会收到 405 错误。这可能是权限问题吗?
    • HTTP 1.0,假设在正文之后关闭
    • @Rusty 如果您在执行 POST 而不是 GET 时得到了这个,那么您的 sn-p 与您的实际代码不同 - 您在 Confirm 类上定义了 get() ,而不是post()
    【解决方案3】:

    @Rusty 我认为您应该更改表单提交方法以获取或发布,并确保您在 application.py 中也写了相同的内容。或者你可以做这样的事情

    class Confirm(webapp.RequestHandler):
        def post(self):    
           self.response.headers['Content-Type'] = 'text/plain'
           self.response.out.write('You have confirmed!')
        def get(self):
           return Confirm.post(self)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-02-06
      • 2011-05-12
      • 1970-01-01
      • 2011-09-11
      • 2015-11-06
      • 2013-07-30
      • 2014-03-01
      相关资源
      最近更新 更多