我正在使用WebApp2作为框架在Python中开发Web应用程序. 我无法通过填写表格来提交http POST请求参数.
I'm developing a web app in Python using WebApp2 as framework. I can't get the http POST request parameters submitted by filling a form.
那是我创建的表单的HTML代码
That's the HTML code of the form I created
<html> <head> <title>Normal Login Page </title> </head> <body> <form method="post" action="/loginN/" enctype="text/plain" > eMail: <input type="text" name="eMail"><br/> password: <input type="text" name="pwd"><br/> <input type="submit"> </form> </body>那是按下提交按钮后POST请求的结果
That's the result of the POST request after pressing the submit button
POST /loginN/ HTTP/1.1 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8 Accept-Language: it-IT,it;q=0.8,en-US;q=0.6,en;q=0.4 Cache-Control: max-age=0 Content-Length: 33 Content-Type: text/plain Content_Length: 33 Content_Type: text/plain Cookie: session=############ Host: ########### Origin: ########### Referer: ############ Upgrade-Insecure-Requests: 1 User-Agent: Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36 X-Appengine-City: ####### X-Appengine-Citylatlong: ######## X-Appengine-Country: ## X-Appengine-Region: ## X-Cloud-Trace-Context: ## eMail=mymail@email pwd=mypwd那是POST请求处理程序的代码
That's the code of the POST request handler
class loginN(BaseHandler): def post(self): w = self.response.write self.response.headers['Content-Type'] = 'text/html' logging.info(self.request) logging.info(self.request.POST.get('eMail')) logging.info(self.request.POST.get('pwd')) email = self.request.POST.get('eMail') pwd = self.request.POST.get('pwd') w('<html>') w('<head>') w('<title>Data Page </title>') w('</head>') w('<p>Welcome! Your mail is: %s</p>' % email) w('<p>Your pwd is: %s</p>' % pwd) w('</body>')BaseHandler是webapp2.RequestHandler的扩展,用于处理会话(我也尝试过使用webapp2.RequestHandler,并且得到了相同的结果).
BaseHandler is webapp2.RequestHandler extended for handling sessions (i tried with webapp2.RequestHandler also and i got the same results).
我每次得到的都是两个参数都为空.
What I get every time is None for both parameters.
有关如何解决问题的任何建议?我也尝试过self.request.get,而不是self.request.POST.get,但是也没有用(我也没有得到None)
Any suggestions about how to solve the problem? I tried self.request.get also, instead self.request.POST.get, but it didn't work too (i didn't get None neither)
推荐答案尝试从表单中删除enctype="text/plain"属性,然后使用self.request.POST.get('eMail')和self.request.POST.get('pwd').
Try removing the enctype="text/plain" attribute from the form, and then use self.request.POST.get('eMail') and self.request.POST.get('pwd').
删除enctype="text/plain"的原因是因为您希望enctype为"text/html"(这是默认设置),以便webapp2可以将表单读取为html表单.仅将其设置为"text/plain"时,表单的输出作为纯文本包含在请求的主体中,这是您打印请求时所看到的.如果使用"text/plain",则可以使用以下命令以字符串形式访问表单的输出:
The reason why removing enctype="text/plain" works is because you want the enctype to be "text/html" (which is the default) in order for webapp2 to read the form as an html form. When it is just set to "text/plain", the form's output is contained in the body of the request as just text, which is what you saw when you printed out the request. If you use "text/plain", then you could access the form's output as a string by using:
form_string = str(self.request.body),然后您可以解析该字符串以获取键值对.正如您已经知道的那样,仅将enctype设置为html即可获得标准的http形式的功能.
and then you could parse that string to get the key-value pairs. As you're already aware though, it is easier just to set the enctype to html to get the standard http-form functionality.
我在文档中找不到具体的enctype信息,但是如果您对请求对象有其他疑问,建议阅读 Webob文档用于请求对象. Webapp2使用Webob请求,因此可以使用文档来了解您的请求对象.
I couldn't specifically find enctype information in the documentation, but if you have other questions about the request object I suggest reading the Webob Documentation for a request object. Webapp2 uses Webob requests, so that documentation is the place to go to understand your request obejct.
更多推荐
无法获取POST参数
发布评论