2

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

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)

日历.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天。无论我尝试用我的代码做什么,我仍然无法让它在互联网上运行。提前致谢!

4

1 回答 1

3

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

尝试改变

from calendar import *

from .calendar import *

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

于 2013-09-18T12:27:12.957 回答