问题描述
在从表单发布数据并使用webapp2处理数据时遇到一些麻烦.
Having some trouble posting data from form and handling it with webapp2.
我不确定一般如何在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.
我的表单在"/schedule/create-consult"页面上.并且我最初正在测试将前两个字段提交到同一页面(即,名字和姓氏发布到/schedule/create-consults).
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).
我的表单如下
<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不允许使用的方法-此资源不允许使用POST方法.
我的路线看起来像这样
app = webapp2.WSGIApplication([
('/', MainPage),
('/schedule', SchedulePage),
('/consults', ConsultsPage),
('/schedule/create-consult', CreateConsultPage),
('/consults/john-smith-030617-0930', JohnSmithPage)
], debug=True)
我的CreateConsultsPage处理程序如下
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())
我的app.yaml如下:
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
推荐答案
您正在使用post方法提交表单.您必须在处理程序类中定义post函数,以获取提交的表单数据.这样可以解决您的问题.
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')
这篇关于webapp2-如何发布表单数据-App Engine的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!