python post and get commands -
i have been having trouble post function. application design people enter birthday , if put in month, day, , year in right post "thanks! that's totally valid day" if not date post "that doesn't valid me, friend", not refreshes self each time push submit. in code did go wrong post function or , post function?
import webapp2 form=""" <form method="post"> birthday? <br> <label>month<input type="type" name="month"></label> <label>day<input type="type" name="day"></label> <label>year<input type="type" name="year"></label> <div style="color: red">%(error)s</div> <br> <br> <input type="submit"> </form> """ class mainpage(webapp2.requesthandler): def write_form(self, error=""): self.response.out.write(form % {"error": error}) def get(self): self.write_form() def valid_year(year): if year , year.isdigit(): year = int(year) if year > 1900 , year < 2020: return year def valid_day(day): if day , day.isdigit(): day = int(day) if day > 0 , day <= 31: return day months = ['janurary', 'february', 'march', 'april', 'may', 'june', 'july', 'august', 'september', 'october', 'november', 'december'] month_abbvs = dict((m[:3].lower(),m) m in months) def valid_month(month): if month: short_month = month[:3].lower() return month_abbvs.get(short_month) 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.write_form("that doesn't valid me, friend.") else: self.response.out.write("thanks! that's totally valid day!") app = webapp2.wsgiapplication([('/',mainpage)], debug=true)
i download python idle , use instead of notpad++. when push submit got:
internal server error server has either erred or incapable of performing requested operation. traceback (most recent call last): file "c:\program files (x86)\google\google_appengine\lib\webapp2-2.5.2\webapp2.py", line 1535, in __call__ rv = self.handle_exception(request, response, e) file "c:\program files (x86)\google\google_appengine\lib\webapp2-2.5.2\webapp2.py", line 1529, in __call__ rv = self.router.dispatch(request, response) file "c:\program files (x86)\google\google_appengine\lib\webapp2-2.5.2\webapp2.py", line 1278, in default_dispatcher return route.handler_adapter(request, response) file "c:\program files (x86)\google\google_appengine\lib\webapp2-2.5.2\webapp2.py", line 1102, in __call__ return handler.dispatch() file "c:\program files (x86)\google\google_appengine\lib\webapp2-2.5.2\webapp2.py", line 572, in dispatch return self.handle_exception(e, self.app.debug) file "c:\program files (x86)\google\google_appengine\lib\webapp2-2.5.2\webapp2.py", line 570, in dispatch return method(*args, **kwargs) file "c:\users\ajper_000\desktop\engineapp\main.py", line 66, in post user_month = valid_month(self.request.get('month')) nameerror: global name 'valid_month' not defined
try replace
<form>
with
<form method="post">
forms, default, use requests, while request handler expects post request.
edit: appears have multiple problems:
- the original problem, above anwser.
- your indentation wrong, causing
post
method undefined (or defined withinget
method maybe) - the
valid_*
methods undefined - need define them.
Comments
Post a Comment