简体   繁体   中英

Just getting started on Google App Engine and being greeted with a blank page

I'm currently in Udacity's CS253 (Web Development) course, and I'm getting through the second homework project (the ROT13) website. I paid attention to the lecture and I think I have a good grip on the code in the file naisho.py , which is here:

import webapp2
import codecs
import cgi

form = """
<form method="post">
    Tell me a secret...
    <br>
    <input type="text" name="secret" value="%(secret)s">

    <div style="color: red">%(error)s</div>

    <br>
    <br>

    <input type="submit">
</form>
"""

class MainPage(webapp2.RequestHandler):

    def process(s):
      return codecs.encode(s, "rot_13")

    def escape(s):
      return cgi.escape(s, quote = True)

    def write_form(self, error="", secret=""):
      self.response.out.write(form % {"error": error, 
                                      "secret": escape(secret)})

    def get(self):
        #self.response.headers['Content-Type'] = 'text/plain'
        self.write_form()

        #self.response.headers['Content-Type'] = 'text/plain'
        #self.response.write(self.request)

    def post(self):
        self.redirect('/thanks')

  class ThanksHandler(webapp2.RequestHandler):
    def get(self):
      n = process(secret)
      self.response.out.write(n)

    def escape(s):
      return cgi.escape(s, quote = True)

application = webapp2.WSGIApplication([
    ('/', MainPage), ('/thanks', ThanksHandler)], debug=True)

and file app.yaml provided:

application: naisho
version: 1
runtime: python27
api_version: 1
threadsafe: true

handlers:
- url: /.*
  script: naisho.application

I navigate to the folder in the terminal (I use OSX 10.9.3), enter dev_appserver.py --port=9999 . to load localhost:9999 , but get a blank page when I load it into my browser. I am absolutely puzzled as to why. Could someone point me in the right direction and also possibly point me in the direction of a reference to better diagnose these sorts of things? Thank you folks kindly in advance.

First, your code contains some bugs and indentation problems.

The bugs is that escape and process are methods of the class, so they should contain self as their first argument and be called as self.escape or self.process. An alternative would be to move those methods out of the class as functions.

Once you do that, in the console you shouldn't see any error hidden in Stack Traces except for an annoying 303 error.

For whatever reason, I also get those 303 errors when running on the dev server, but if you deploy it to an App Engine instance, it works smoothly.

You could try to deploy to the App Engine first, and look into their logs to catch your bugs/indentation as it makes it easier to spot them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM