我正在使用python和Google App引擎制作基本网页。
问题出在下面一行
self.self_form("That doesn't look valid to me, friend.")
当我保存文件并刷新网页时。我收到以下错误
第94行,在帖子中 self.write_form(“朋友,这对我来说似乎无效。”) TypeError:write_form()最多接受1个参数(给定2个参数)
import webapp2
form="""
<form method="post">
When is your birthday????
<br>
<br>
<label> Day
<input type="text" name="day">
</label>
<label> Month
<input type="text" name="month">
</label>
<label> Year
<input type="text" name="year">
</label>
<div style="color: red">%(error)s </div
<br>
<br>
<input type="submit">
</form>
"""
months = ['January',
'February',
'March',
'April',
'May',
'June',
'July',
'August',
'September',
'October',
'November',
'December']
month_abbvs = dict((m[:3].lower(),m) for m in months)
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)
def valid_day(day):
if day.isdigit():
return int(day) if int(day) in range(1,32) else None
else:
return None
def valid_year(year):
if year.isdigit():
return int(year) if int(year) in range(1900, 2021) else None
else:
return None
class MainPage(webapp2.RequestHandler):
def write_form(error=""):
self.response.out.write(form % {"error": error} )
def get(self):
#self.response.headers['Content-Type'] = 'text/plain'
self.write_form()
def post(self):
#self.response.headers['Content-Type'] = 'text/plain'
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_day and user_year and user_month):
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)
这里可能是什么问题?
该代码来自Udacity Web开发课程,第5课,视频39
答案 0 :(得分:2)
应将其定义为def write_form(self, error=""):