当我尝试提交此DJango表单时,收到内部服务器错误“TypeError:valid_month()恰好接受1个参数(给定2个)”。在我看来,我只是将一个参数传递给valid_month(),而不是两个。您能帮我了解我在做什么错吗?我正在使用Google App Engine启动器对此进行测试。
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>
"""
表格
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)
提交表单时,我收到以下回溯:
>追溯(最近一次通话):文件
>“/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(请求,响应,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(请求,响应)文件“/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(请求,响应)文件“/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'))TypeError:valid_month()恰好接受1个参数(给定2个)
最佳答案
快速而肮脏的解决方案是将self
参数添加到valid_day
,valid_month
和valid_year
函数:
class MainHandler(webapp2.RequestHandler):
def valid_day(self, day):
if day.isdigit() and int(day) in range(1, 32):
return int(day)
def valid_month(self, 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(self, year):
if year.isdigit() and int(year) in range(1900, 2021):
return year
...
但是,甚至更好的方法是将
valid_day
,valid_month
和valid_year
移到webapp2.RequestHandler
之外,因为只有在与类相关且需要实例的情况下,才应定义类方法。在您的情况下,这些辅助函数仅是验证日期部分-不应将它们定义为webapp2.RequestHandler
类上的方法。然后,不使用self.
调用这些函数: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
class MainHandler(webapp2.RequestHandler):
def get(self):
self.response.headers['Content-Type'] = 'text/html'
self.response.out.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_year):
self.response.out.write(form)
else:
self.response.out.write("You entered a valid date")
关于django - Django内部服务器错误(接受1个参数(给定2个)),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18324894/