使用Python在Google App Engine中提交表单帖子后显示

时间:2011-03-21 12:47:38

标签: python google-app-engine

页面内容在表单发布后未显示,但在直接查看页面时显示。我有一个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>

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

3 个答案:

答案 0 :(得分: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()

答案 1 :(得分:0)

我会测试你的断言POST确认可以正确使用curl发布一些东西:

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

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

似乎你要么没有提交你认为自己的东西,要么你的POST处理程序没有像你想象的那样工作。上述两个步骤都应该有助于澄清哪个是哪个。

答案 2 :(得分:0)

@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)
相关问题