我正在进行Udacity的网络开发课程,而我在第2课第35课中遇到了问题。这是我的代码。
import webapp2
form="""
<form method="post">
What is your birthdate?
<br>
<label> Month
<input type="text" name="month">
</label>
<label> Day
<input type="text" name="day">
</label>
<label> Year
<input type="text" name="year">
</label>
<br>
<br>
<input type="submit">
</form>
"""
class MainPage(webapp2.RequestHandler):
months = ['January','February','March','April','May','June','July',
'August','September','October','November','December']
months_abbvs = dict((m[:3].lower(), m) for m in months)
def valid_month(month):
if month:
short_month = month[:3].lower()
if short_month in months:
return months_abbvs.get(short_month)
def valid_day(day):
if day and day.isdigit():
day = int(day)
if day > 0 and day <= 31:
return day
def valid_year(year):
if year and year.isdigit():
year = int(year)
if year >= 1900 and year <= 2020:
return year
def get(self):
self.response.write(form)
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_day):
self.response.out.write(form)
else:
self.response.write("Thanks! That's a totally calid day!")
app = webapp2.WSGIApplication([
('/', MainPage)], debug=True)
我通过谷歌应用引擎运行它。当我在浏览器中单击表单上的提交时,我得到:
This page isn’t working
localhost is currently unable to handle this request.
HTTP ERROR 500
当我查看运行服务器的cmd提示时,我收到错误:
INFO 2017-11-01 22:09:22,153 module.py:821] default: "GET / HTTP/1.1"
200 382
ERROR 2017-11-02 02:09:46,470 webapp2.py:1528] global name
'valid_month' is not defined
Traceback (most recent call last):
File "C:\Users\mmelv\AppData\Local\Google\Cloud SDK\google-cloud-
sdk\platform\google_appengine\lib\webapp2-2.3\webapp2.py", line 1511, in
__call__
rv = self.handle_exception(request, response, e)
File "C:\Users\mmelv\AppData\Local\Google\Cloud SDK\google-cloud-
sdk\platform\google_appengine\lib\webapp2-2.3\webapp2.py", line 1505, in
__call__
rv = self.router.dispatch(request, response)
File "C:\Users\mmelv\AppData\Local\Google\Cloud SDK\google-cloud-
sdk\platform\google_appengine\lib\webapp2-2.3\webapp2.py", line 1253, in
default_dispatcher
return route.handler_adapter(request, response)
File "C:\Users\mmelv\AppData\Local\Google\Cloud SDK\google-cloud-
sdk\platform\google_appengine\lib\webapp2-2.3\webapp2.py", line 1077, in
__call__
return handler.dispatch()
File "C:\Users\mmelv\AppData\Local\Google\Cloud SDK\google-cloud-
sdk\platform\google_appengine\lib\webapp2-2.3\webapp2.py", line 547, in
dispatch
return self.handle_exception(e, self.app.debug)
File "C:\Users\mmelv\AppData\Local\Google\Cloud SDK\google-cloud-
sdk\platform\google_appengine\lib\webapp2-2.3\webapp2.py", line 545, in
dispatch
return method(*args, **kwargs)
File "C:\Users\mmelv\AppData\Local\Google\Cloud SDK\python-docs-
samples\appengine\standard\hello_world\main.py", line 50, in post
user_month = valid_month(self.request.get('month'))
NameError: global name 'valid_month' is not defined
ERROR 2017-11-02 02:09:46,470 wsgi.py:279]
Traceback (most recent call last):
File "C:\Users\mmelv\AppData\Local\Google\Cloud SDK\google-cloud-
sdk\platform\google_appengine\google\appengine\runtime\wsgi.py", line
267, in Handle
INFO 2017-11-01 22:09:46,477 module.py:821] default: "POST /
HTTP/1.1" 500 -
result = handler(dict(self._environ), self._StartResponse)
File "C:\Users\mmelv\AppData\Local\Google\Cloud SDK\google-cloud-
sdk\platform\google_appengine\lib\webapp2-2.3\webapp2.py", line 1519, in
__call__
response = self._internal_error(e)
File "C:\Users\mmelv\AppData\Local\Google\Cloud SDK\google-cloud-
sdk\platform\google_appengine\lib\webapp2-2.3\webapp2.py", line 1511, in
__call__
rv = self.handle_exception(request, response, e)
File "C:\Users\mmelv\AppData\Local\Google\Cloud SDK\google-cloud-
sdk\platform\google_appengine\lib\webapp2-2.3\webapp2.py", line 1505, in
__call__
rv = self.router.dispatch(request, response)
File "C:\Users\mmelv\AppData\Local\Google\Cloud SDK\google-cloud-
sdk\platform\google_appengine\lib\webapp2-2.3\webapp2.py", line 1253, in
default_dispatcher
return route.handler_adapter(request, response)
File "C:\Users\mmelv\AppData\Local\Google\Cloud SDK\google-cloud-
sdk\platform\google_appengine\lib\webapp2-2.3\webapp2.py", line 1077, in
__call__
return handler.dispatch()
File "C:\Users\mmelv\AppData\Local\Google\Cloud SDK\google-cloud-
sdk\platform\google_appengine\lib\webapp2-2.3\webapp2.py", line 547, in
dispatch
return self.handle_exception(e, self.app.debug)
File "C:\Users\mmelv\AppData\Local\Google\Cloud SDK\google-cloud-
sdk\platform\google_appengine\lib\webapp2-2.3\webapp2.py", line 545, in
dispatch
return method(*args, **kwargs)
File "C:\Users\mmelv\AppData\Local\Google\Cloud SDK\python-docs-
samples\appengine\standard\hello_world\main.py", line 50, in post
user_month = valid_month(self.request.get('month'))
NameError: global name 'valid_month' is not defined
据我所知,问题在于 user_month = valid_month(self.request.get(&#39; month)) 虽然我已尝试将缩进从空格更改为制表符并返回空格,重写整个代码,然后重新启动服务器,但都无济于事。有人帮帮我...
答案 0 :(得分:0)
当从它们所属的类中调用成员函数时,需要在self.
前面加上它们。此外,您的所有成员函数(例如valid_month()
,valid_day()
和valid_year()
都必须self
作为其实际参数的第一个值。此外,对months
和months_abbvs
等静态类变量的任何引用也必须以self.
开头。
此外,valid_month()
,valid_day()
和valid_year()
的返回值可能存在逻辑错误。每个都返回一个字符串,但post()
中的条件是评估这三个是否True
。此外,valid_months()
中的条件可能是为了检查short_month
是否在months_abbvs
而不是months
。
尝试使用以下内容替换当前的post()
函数:
def post(self):
user_month = self.valid_month(self.request.get("month"))
user_day = self.valid_day(self.request.get("day"))
user_year = self.valid_year(self.request.get("year"))
if not(user_month and user_day and user_day):
self.response.out.write(form)
else:
self.response.write("Thanks! That's a totally calid day!")
然后更改以下函数标题:
对于valid_month()
,请使用以下内容替换现有函数:
def valid_month(self, month):
if month:
short_month = month[:3].lower()
if short_month in self.months_abbvs:
return True
对于valid_day()
,请使用以下内容替换现有函数:
def valid_day(self, day):
if day and day.isdigit():
day = int(day)
if day > 0 and day <= 31:
return True
对于valid_year()
,请使用以下内容替换现有函数:
def valid_year(self, year):
if year and year.isdigit():
year = int(year)
if year >= 1900 and year <= 2020:
return True