Python Post And Get Commands
Solution 1:
Try to replace
<form>
with
<formmethod="POST">
Forms, by default, use GET requests, while your request handler expects a POST request.
EDIT: It appears you have multiple problems:
- The original problem, to which the above is the anwser.
- Your indentation was wrong, causing the
post
method to be undefined (or defined within theget
method maybe) - The
valid_*
methods are undefined - you need to define them.
Solution 2:
You should return true in the functions of valid_month, valid_year, and valid_day. Just like this code
import webapp2
months = ['Janurary',
'February',
'March',
'April',
'May',
'June',
'July',
'August',
'September',
'October',
'November',
'December']
month_abbvs = dict((m[:3].lower(), m) for m in months)
defvalid_month(user_month):
if user_month:
short_month = user_month[:3].lower()
return month_abbvs.get(short_month)
returnTruedefvalid_day(user_day):
if user_day and user_day.isdigit():
user_day = int(user_day)
if user_day > 0and user_day <= 31:
return user_day
returnTruedefvalid_year(user_year):
if user_year and user_year.isdigit():
user_year = int(user_year)
if user_year > 1900and user_year < 2020:
return user_year
returnTrue
form = """
<form method='POST'>
<h1>What is your birthday?</h1>
<br>
<label>Month
<input type="text" name="month">
</label>
<label>day
<input type="text" name="day">
</label>
<label>year
<input type="text" name="year">
</label>
<input type="submit">
</form>
"""classMainHandler(webapp2.RequestHandler):
defget(self):
self.response.write(form)
defpost(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'))
ifnot (user_year and user_month and user_day):
self.response.out.write(form)
self.response.out.write("Please enter valid a function")
else:
self.response.out.write('tThanks for that')
app = webapp2.WSGIApplication([('/', MainHandler)], debug=True)
Because when the function valid_month is called it should return True to display the error message in browser.
Solution 3:
The cause of the 405 is right at the bottom of the stack trace.
NameError: global name 'valid_month'isnot defined
suggests that you don't have a function named valid_month
. If you don't, then write one. If you do, then post it.
Solution 4:
Alexander, I am doing the same course at udacity and I hit the same problem. But before going to the stage where you added the functions to check the user input I just checked the output in the browser right after the teacher changed the code afte the combo box. I had the same problem of getting "405 Method Not Allowed" along with the message on the next line "The method POST is not allowed for this resource. I scanned the code and compared it line by line and word by word and character by character with that code in the video "https://www.udacity.com/course/viewer#!/c-cs253/l-48736183/m-48714317" but it did not help.
Then I hit the accompanying wiki for the course and copied exactly the 4 lines that I doubted has the problem. And suddenly it worked. I used different on line text comaprison tools to find out what was the difference but could not figure out what was the problem. I copy and paste here both the versions below:
Version 1: The verison that did not work and gave the 405 error:
defget(self):
self.response.out.write(form)
defpost(self):
self.response.out.write("Thanks! that's a totally valid day!")
Version 2: The version that works:
defget(self):
self.response.out.write(form)
defpost(self):
self.response.out.write("Thanks! That's a totally valid day!")
I can't see the differenece between these two pieces of code but even then version 1 gives error but version 2 gives the desired output. Although I have exactly copied and pasted the two versions ( given above) of the code from Notepade++ I still took screenshots so that the readers do not say that I might have made some indentation problem. Here are the screenshots of the two versions:
Version 1: That did not work:
Verson 2: That works:
Although the problem is solved at the moment but I wonder what is causing the problem! Any python guru can explain why?
Solution 5:
Declaring the methods outside the class and before the class declaration and after the end of the form works for me.The code was like this.
import webapp2
form="""
<form method='POST' >
What is your birthday
<br>
<label>Month
<input type="text" name="month">
</label>
<label>day
<input type="text" name="day">
</label>
<label>year
<input type="text" name="year">
</label>
<input type="submit">
</form>
"""
months = ['January',
'February',
'March',
'April',
'May',
'June',
'July',
'August',
'September',
'October',
'November',
'December']
month_abbvs = dict((m[:3].lower(),m) for m in months)
def valid_month(month):
if month:
shortMonth=month[:3].lower()
return month_abbvs.get(shortMonth)
def valid_day(day):
if dayand day.isdigit():
day=int(day)
if day>0andday<=31:
returnday
def valid_year(year):
if yearand year.isdigit():
year=int(year)
if year>=1900andyear<=2020:
returnyear
class MainHandler(webapp2.RequestHandler):
def get(self):
# self.response.headers['Content-Type']='Text/HTML'
self.response.write(form)
def post(self):
user_moth = valid_month(self.request.get('month'))
user_day = valid_day(self.request.get('day'))
user_year = valid_year(self.request.get('year'))
#self.response.out.write("Thanks... ")
if not (user_year and user_moth and user_day):
self.response.out.write(form)
else:
self.response.out.write('tThanks for that')
app = webapp2.WSGIApplication([
('/', MainHandler),
], debug=True)
Hope it will help.Otherwise you can pass additional parameter self to these function definition and call it using instance variable
Post a Comment for "Python Post And Get Commands"