Reputation: 5540
I wrote a code for validating a form. It works perfectly on my local host. However when I deploy it on the internet, I get an error. Here is the entire code and the error associated with it:
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
This is the error that I get after uploading on the internet:
Internal Server Error
The server has either erred or is incapable of performing the requested operation.
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
Can anyone help me with this? I am stuck on this for 3 days now. No matter what I try to do with my code, I still couldn't make it run on the internet. Thanks in advance!
Upvotes: 2
Views: 511
Reputation: 4006
Not familiar with GAE but it will probably be to do with the order in which packages are being imported.
Try changing
from calendar import *
to
from .calendar import *
In the first case it might be importing the system calendar package. In the second case you're asking it to import the local package (your calendar.py).
Upvotes: 3