webapp2 self.request.get返回nil

时间:2014-11-06 05:45:02

标签: python html forms google-app-engine webapp2

我正在开发一个非常简单的python程序,我向用户提供一个表单来选择Android或IPhone并输入一个电子邮件地址。 html和python代码包含在下面。无论出于何种原因,我都没有从self.request.get获得任何数据。

HTML代码段

 <div id="select_OS">
                <form action="/optin" role="form" class="form-horizontal" method="post" enctype="text/plain">
                    <div class="form-group">
                    <div class="input_contents">
                    <ul id="phone_list" class="list-unstyled">
                        <li><label><input type="radio" class="" name="device" value="android"> Android</label></li>
                        <li><label><input type="radio" class="" name="device" value="iphone"> IPhone</label></li>
                    </ul>
                    </div>
                    </div>
                    <div class="container">

                        <p class="info_txt_bottom"><span class="glyphicon glyphicon-asterisk"></span>
                        Instructions for
                        </p>

                        </<div>

                            <div class="form-group email_content">
                                <div class="col-xs-6 col-sm-8 col-md-8 enteremail"><input type="email" class="form-control input-lg " id="email" name="email" placeholder="Enter Your Email" required></div>
                                <div class="col-xs-6 col-sm-4 col-dm-4" ><div class="sentbutton">
                               <button type="submit" class="btn btn-success input-lg colorBlack">Send</button>
                               </div></div>
                            </div>




                </form>
            </div>

Python代码段

class ReadForm(webapp2.RequestHandler):
    def post(self):
        logging.warning('The email is %s',self.request.get('email'))
        logging.warning('The device is %s',self.request.get('device'))

app = webapp2.WSGIApplication([
    ('/', MainHandler),
    ('/optin', ReadForm),
], debug=True)

同样,日志记录仅打印“电子邮件是”,而不包括用户输入的实际电子邮件地址。 “设备是”的问题相同。为什么表单数据没有传递?

更新

当我logging.warning('The REQUEST %s',self.request)时,我得到了

/optin HTTP/1.1
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Language: en-US,en;q=0.8
Cache-Control: max-age=0
Content-Length: 31
Content-Type: text/plain
Content_Length: 31
Content_Type: text/plain
Dnt: 1

…

device=android
email=me@hellothere.com

注意最后两行。这些是我试图用self.request.get提取的数据,但我一直在空洞。

3 个答案:

答案 0 :(得分:1)

唯一跳出来的是输入字段缺少value=""属性。如果你正在使用html5,这可能是一个问题,至少根据严格阅读spec.

答案 1 :(得分:0)

在下一行cgi.escape(self.request.get('email'))尝试self.request.get('email'),而不是device,当然import cgi

答案 2 :(得分:0)

问题是你的enctype说enctype="text/plain"。只需删除它,它应该工作。我不知道删除enctype有任何负面影响。所以,如果其他人知道一个不好的理由,请分享。但基本上,一旦你删除了这个片段,你的数据应该很好,你的日志应该是

Content-Type: application/x-www-form-urlencoded