当我尝试提交此 DJango 表单时,我收到一个内部服务器错误“TypeError:valid_month() 恰好需要 1 个参数(给定 2 个)”。在我看来,我只将一个参数传递给 valid_month(),而不是两个。你能帮我理解我在这里做错了什么吗?我正在使用谷歌应用引擎启动器来测试这个。
import webapp2
form="""
<form method="post">
What is your birthday?<br>
<label>
<input type="text" name="month">
</label>
<label>
<input type="text" name="day">
</label>
<label>
<input type="text" name="year">
</label>
<br><br>
<input type="submit">
</form>
"""
表格.py
class MainHandler(webapp2.RequestHandler):
def valid_day(day):
if day.isdigit() and int(day) in range(1, 32):
return int(day)
def valid_month(month):
months = {'jan':'January', 'feb': 'February', 'mar':'March', 'apr':'April','may':'May',
'jun':'June', 'jul': 'July', 'aug': 'August', 'sep': 'September',
'oct': 'October', 'nov': 'November', 'dec': 'December'}
m = month.lower()[:3]
if m in months:
return months[m]
def valid_year(year):
if year.isdigit() and int(year) in range(1900, 2021):
return year
def get(self):
self.response.headers['Content-Type'] = 'text/html'
self.response.out.write(form)
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_year):
self.response.out.write(form)
else:
self.response.out.write("You entered a valid date")
app = webapp2.WSGIApplication([('/', MainHandler)], debug=True)
提交表单时,我收到以下回溯:
> Traceback(最近一次调用最后一次):文件 > "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.5.2/webapp2.py", > 第 1535 行,在 __call__ 中 > rv = self.handle_exception(request, response, e) 文件“/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.5.2/webapp2.py” , > 第 1529 行,在 __call__ 中 > rv = self.router.dispatch(request, response) 文件“/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.5.2/webapp2.py” , > 第 1278 行,在 default_dispatcher 中 > 返回 route.handler_adapter(request, response) 文件“/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.5.2/webapp2.py”, > 第 1102 行,在 __call__ 中 > 返回 handler.dispatch() 文件“/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.5.2/webapp2.py”, > 第 572 行,调度中 > 返回 self.handle_exception(e, self.app.debug) 文件“/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.5.2/webapp2.py ", > 第 570 行,正在调度中 > 返回方法(*args, **kwargs) 文件“/Users/macuser/Documents/UdactyCS253/HelloWorld/hello/main.py”,行 > 58,在岗 > user_month = self.valid_month(self.request.get('month')) 类型错误:valid_month() 只需要 1 个参数(给定 2 个)