开发者

Getting display after form post submission in Google App Engine in Python

开发者 https://www.devze.com 2023-02-18 13:49 出处:网络
Content of page not displayed after a form post, but displayed when directly viewing the page. I have a Python App Engine piece of code that is attempting to direct to a ne开发者_如何学Pythonw page an

Content of page not displayed after a form post, but displayed when directly viewing the page. I have a Python App Engine piece of code that is attempting to direct to a ne开发者_如何学Pythonw page and display a programatically defined (i.e. in the code, not html) piece of text. However up pressing the submit button of the form I get a blank page and no error messages.

I've been using the Google App engine code examples. The form just just takes some options, but I'm not even collecting anything from it and should go to the new page, however it does not and I cannot find out where it might be going wrong.

I have

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))

and

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()

and in the 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>

I want to know why if I submit the form, I do not get the You have confirmed! message, but if I go to /confirm I do. Thanks.


Your code runs pretty smoothly; probably you have not implemented correctly the post method for some weird indentation error (this would explain the 405 error).

Copy and paste my application.py and try again:

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()


I would test your assertion that POST to Confirm works correctly using curl to post something:

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

If that works fine, then I would use HttpFox (a FF extension) to see what's sent to the server.

Seems you're either not submitting what you think you are, or you're POST handler is not working as you think it is. Both of the above steps should help clarify which is which.


@Rusty i think you should either change your form submission method to get or post and make sure you have written the same in the application.py as well. or you can do something like this

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)
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号