Reputation: 11790
I am working on a very simple python program where I present a user a form to choose either Android or IPhone and to enter an email address. The html and the python codes are included below. For whatever reason I am not getting any data from self.request.get
.
HTML snippet
<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 snippet
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)
Again, the logging only prints “The email is” without including the actual email address that the user enters. The same issue for "The device is". Why is the form data not being passed along?
UPDATE
When I do logging.warning('The REQUEST %s',self.request)
, I get
/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 protected]
Notice the last two lines. These are the data I have been trying to extract with self.request.get
, but I keep getting empty back.
Upvotes: 0
Views: 459
Reputation: 2434
The problem is that your enctype says enctype="text/plain"
. Simply remove that and it should work. I am not aware of any negative effects of removing enctype. So if someone else here knows a reason that's a bad idea, please share. But basically, once you remove that snippet, your data should come through fine and your log should read
Content-Type: application/x-www-form-urlencoded
Upvotes: 0
Reputation: 3709
Try cgi.escape(self.request.get('email'))
, not self.request.get('email')
same for device
in the next line and import cgi
of course
Upvotes: 0
Reputation: 24966
The only thing that jumps out is the lack of value=""
attribute on your input field. If you're using html5, that may be a problem, at least according to strict read of the spec.
Upvotes: 1