代码在localhost上工作,但在使用Google App Engine部署时无效

时间:2013-09-18 12:07:25

标签: python google-app-engine

我写了一个用于验证表单的代码。它在我的本地主机上完美运行。但是,当我在互联网上部署它时,我收到一个错误。以下是整个代码以及与之相关的错误:

import webapp2
from calendar import *
import cgi

form = """
<form method = "post">
<label>Month<input type = "text" name = "mon" value = "%(month)s"></label>
<label>Day<input type = "text" name = "dy" value = "%(day)s"></label>
<label>Year<input type = "text" name = "yr" value = "%(year)s"></label>
<div style="color:red">%(error)s</div>
<br>
<br>
<input type = "submit">
</form>
"""

def escape_html(s):
    return cgi.escape(s, quote = True)
class MainHandler(webapp2.RequestHandler):
    error = ""
    def write_form(self, error, month, day, year):
        self.response.write(form %{"error":error, "month":escape_html(month), "day":escape_html(day), "year":escape_html(year)})
    def get(self):
    #self.response.headers['Content-Type'] = 'text/plain'
        self.write_form("", "", "", "")
    def post(self):
        mon = valid_month(self.request.get("mon"))  
        dy = valid_day(self.request.get("dy"))  
        yr = valid_year(self.request.get("yr"))
        month = self.request.get("mon")
        day = self.request.get("dy")
        year = self.request.get("yr")
        if not(mon and dy and yr):
            self.write_form("Please refill the form with correct data!", month, day, year)
        else:
            self.redirect("/thanks")

class ThanksHandler(webapp2.RequestHandler):
    def get(self):
        self.response.write("Thanks for submitting your data!") 

app = webapp2.WSGIApplication([('/', MainHandler), ('/thanks', ThanksHandler)],    debug=True)

calendar.py

months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']

def valid_month(month):
    temp = month.capitalize()
    for e in months:
        if e == temp:
            return e
    return None     

def valid_day(day):
    try:
        num = int(day)
    except:
        return None
    if num<=31 and num>0:
        return num
    else:
        return None

def valid_year(year):
    if year and year.isdigit():
        num = int(year)
        if num>1900 and num<2020:
            return num

这是我在互联网上传后得到的错误:

内部服务器错误

服务器出错或无法执行请求的操作。

Traceback (most recent call last):
  File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1535, in __call__
    rv = self.handle_exception(request, response, e)
  File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1529, in __call__
    rv = self.router.dispatch(request, response)
  File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1278, in default_dispatcher
    return route.handler_adapter(request, response)
  File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1102, in __call__
    return handler.dispatch()
  File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 572, in dispatch
    return self.handle_exception(e, self.app.debug)
  File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 570, in dispatch
    return method(*args, **kwargs)
  File "/base/data/home/apps/s~deploymentapp/1.370308128873516940/main.py", line 27, in post
    mon = valid_month(self.request.get("mon"))
NameError: global name 'valid_month' is not defined

任何人都可以帮我吗?我现在被困在这3天了。无论我尝试用我的代码做什么,我仍然无法让它在互联网上运行。提前谢谢!

1 个答案:

答案 0 :(得分:3)

不熟悉GAE,但可能与导入包的顺序有关。

尝试更改

from calendar import *

from .calendar import *

在第一种情况下,它可能正在导入系统日历包。在第二种情况下,您要求它导入本地包(您的calendar.py)。