Reputation: 95
I receive an internal server error "TypeError: valid_month() takes exactly 1 argument (2 given)" when I try and submit this DJango form. It looks to me like I'm only passing one argument to valid_month(), not two. Will you help me understand what I'm doing wrong here? I'm using the google app engine launcher to test this.
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>
"""
forms.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)
I receive the following traceback when submitting the form:
> Traceback (most recent call last): File > "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.5.2/webapp2.py", > line 1535, in __call__ > rv = self.handle_exception(request, response, e) File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.5.2/webapp2.py", > line 1529, in __call__ > rv = self.router.dispatch(request, response) File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.5.2/webapp2.py", > line 1278, in default_dispatcher > return route.handler_adapter(request, response) File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.5.2/webapp2.py", > line 1102, in __call__ > return handler.dispatch() File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.5.2/webapp2.py", > line 572, in dispatch > return self.handle_exception(e, self.app.debug) File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.5.2/webapp2.py", > line 570, in dispatch > return method(*args, **kwargs) File "/Users/macuser/Documents/UdactyCS253/HelloWorld/hello/main.py", line > 58, in post > user_month = self.valid_month(self.request.get('month')) TypeError: valid_month() takes exactly 1 argument (2 given)
Upvotes: 2
Views: 1282
Reputation: 473873
Quick and dirty solution will be to add self
argument to valid_day
, valid_month
and valid_year
functions:
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
...
But, even better would be to move valid_day
, valid_month
and valid_year
outside of webapp2.RequestHandler
because, you should define class methods only if they are relevant to the class and need an instance. In your case, these helper functions are just validating date parts - they should not be defined as methods on webapp2.RequestHandler
class. Then, call these functions without 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")
Upvotes: 1
Reputation: 9472
The Problem You are sending two requests. As you know you are sending the month, but you are also sending a request.
http://www.djangobook.com/en/2.0/chapter07.html
Take a look at that link. It teaches you everything you need to know about forms and calling them.
Solution I would recommend adding another parameter to your function (requests, month)
Upvotes: 0