webapp2 - how to post form data - app engine

拥有回忆 提交于 2019-12-11 04:58:35

问题


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

回答1:


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')



回答2:


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


来源:https://stackoverflow.com/questions/42298851/webapp2-how-to-post-form-data-app-engine

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!