我一直在使用udacity.com来学习编写一个应用程序,该应用程序允许您键入您的年龄以及何时提交它,检查您是否输入了正确的年,月和日。每次我推送提交并在谷歌应用引擎上运行它的本地主机网页它是黑色的。在我的代码中我错过了什么。我检查我的代码,它看起来应该工作。我需要第二双眼睛。
import webapp2
form="""
<form method="post">
What is your birthday?
<br>
<label>Month<input type="type" name="month"></label>
<label>Day<input type="type" name="day"></label>
<label>Year<input type="type" name="year"></label>
<div style="color: red">%(error)s</div>
<br>
<br>
<input type="submit">
</form>
"""
def valid_year(year):
if year and year.isdigit():
year = int(year)
if year > 1900 and year < 2020:
return year
def valid_day(day):
if day and day.isdigit():
day = int(day)
if day > 0 and day <= 31:
return day
months = ['Janurary',
'February',
'March',
'April',
'May',
'June',
'July',
'August',
'September',
'October',
'November',
'December']
month_abbvs = dict((m[:3].lower(),m) for m in months)
def valid_month(month):
if month:
short_month = month[:3].lower()
return month_abbvs.get(short_month)
class MainPage(webapp2.RequestHandler):
def write_form(self, error=""):
self.response.out.write(form % {"error": error})
def get(self):
self.write_form()
self.valid_year(year)
self.valid_day(day)
self.valid_month(month)
def post(self):
user_month = valid_month(self.request.get('month'))
user_day = valid_day(self.request.get('day'))
user_year = valid_year(self.request.get('year'))
if not (user_month and user_day and user_year):
self.write_form("That doesn't look valid to me, friend.")
else:
self.response.out.write("Thanks! That's a totally valid day!")
app = webapp2.WSGIApplication([('/',MainPage)], debug=True)
答案 0 :(得分:1)
在方法&#34; get&#34;中,您调用方法valid_year(),valid_day(),valid_month(),所以:
1)你没有任何参数调用它,但在方法声明中你声明有参数valid_year(年),valid_day(日),valid_month(月),所以如果你不加参数调用它,就会导致错误。
2)我认为你&#34;得到&#34;方法应如下所示:
def get(self):
self.write_form()
当执行get方法时,您不会收到任何参数/数据,因此无需验证。
答案 1 :(得分:1)
您已定义方法帖子如下: def post(self): 但是当你打开标签表格时,你没有定义方法=&#34; post&#34;。 添加方法=&#34;发布&#34;到你的表格标签。
答案 2 :(得分:0)
我逐字尝试了你的代码,我收到以下错误,导致空白屏幕。
ERROR 2015-08-18 21:27:32,377 wsgi.py:263]
Traceback (most recent call last):
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/runtime/wsgi.py", line 240, in Handle
handler = _config_handle.add_wsgi_middleware(self._LoadHandler())
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/runtime/wsgi.py", line 299, in _LoadHandler
handler, path, err = LoadObject(self._handler)
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/runtime/wsgi.py", line 85, in LoadObject
obj = __import__(path[0])
File "/Users/Josh/main.py", line 1
import webapp2
^
IndentationError: unexpected indent
在python中,空白非常重要。您的初始import webapp2
以及month_abbvs = dict((m[:3].lower(),m) for m in months)
似乎有一些不一致的缩进。