Reputation: 1359
Having some trouble posting data from form and handling it with webapp2.
I'm not sure how to handle data from a form in webapp2 in general, including which page to post the data to with the form action.
My form is on the page '/schedule/create-consult'. and I'm initially testing submitting the first two fields to the same page (ie. first name and last name being posted to /schedule/create-consults).
My form looks like this
<form method="post" action="/schedule/create-consult">
<div class="row">
<div class="col-md-6">
<label>First Name</label>
<input class="form-control input-lg" type="text" name="first_name" />
<br/>
</div>
<div class="col-md-6">
<label>Last Name</label>
<input class="form-control input-lg" type="text" name="last_name" />
</div>
<input type="submit" value="save">
</div>
</form>
When I click the Save button I get the message:
405 Method Not Allowed - The method POST is not allowed for this resource.
My routes look like this
app = webapp2.WSGIApplication([
('/', MainPage),
('/schedule', SchedulePage),
('/consults', ConsultsPage),
('/schedule/create-consult', CreateConsultPage),
('/consults/john-smith-030617-0930', JohnSmithPage)
], debug=True)
My handler for CreateConsultsPage looks like this
class CreateConsultPage(webapp2.RequestHandler):
def get(self):
template = JINJA_ENVIRONMENT.get_template('/templates/create-consult.html')
self.response.out.write(template.render())
And my app.yaml is as follows:
runtime: python27
api_version: 1
threadsafe: true
handlers:
- url: /css
static_dir: css
- url: /images
static_dir: images
- url: /js
static_dir: js
- url: /
script: main.app
- url: /schedule
script: main.app
- url: /consults
script: main.app
- url: /schedule/create-consult
script: main.app
- url: /consults/john-smith-030617-0930
script: main.app
libraries:
- name: webapp2
version: latest
- name: jinja2
version: latest
Upvotes: 1
Views: 859
Reputation: 926
You are submitting the form using post method. You have to define the post function in your handler class to get the submitted form data. This will solve your problem.
class CreateConsultPage(webapp2.RequestHandler):
def get(self):
template = JINJA_ENVIRONMENT.get_template('/templates/create-consult.html')
self.response.out.write(template.render())
def post(self):
first_name = self.request.get('first_name')
last_name = self.request.get('last_name')
Upvotes: 2
Reputation: 494
It's trying to make a POST to your app but you do not have a handler configured to receive it.
Where you have your GET handler:
class CreateConsultPage(webapp2.RequestHandler):
def get(self):
dostuf
you also need to have a POST hander as well:
class CreateConsultPage(webapp2.RequestHandler):
def post(self):
dostuff
Upvotes: 1