Django Internal Server Error (takes exactly 1 argument (2 given)) -
i receive internal server error "typeerror: valid_month() takes 1 argument (2 given)" when try , submit django form. looks me i'm passing 1 argument valid_month(), not two. me understand i'm doing wrong here? i'm using google app engine launcher test this.
import webapp2 form=""" <form method="post"> 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() , 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() , 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 , user_day , user_year): self.response.out.write(form) else: self.response.out.write("you entered valid date") app = webapp2.wsgiapplication([('/', mainhandler)], debug=true)
i receive following traceback when submitting 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 1 argument (2 given)
quick , dirty solution add self
argument valid_day
, valid_month
, valid_year
functions:
class mainhandler(webapp2.requesthandler): def valid_day(self, day): if day.isdigit() , 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() , int(year) in range(1900, 2021): return year ...
but, better move valid_day
, valid_month
, valid_year
outside of webapp2.requesthandler
because, should define class methods if relevant class , need instance. in case, these helper functions validating date parts - should not defined methods on webapp2.requesthandler
class. then, call these functions without self.
:
def valid_day(day): if day.isdigit() , 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() , 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 , user_day , user_year): self.response.out.write(form) else: self.response.out.write("you entered valid date")
Comments
Post a Comment