title: HTTP post with GAE (and WTForms)
body: <p>Hi how can I get the variables in a HTTP post with WTForms when the post is done with a <code>blobstoreuploadhandler</code> and preferably also with i18n localized messages for validation?
  This is my code that is not working:</p>
  
  <pre><code>class AdForm(Form):
      name = TextField(_('Name'))
      title = TextField(_('title'))
      text = TextAreaField(_('Text'),widget=TextArea())
      phonenumber = TextField(_('Phone number'))
      phonenumberhide = BooleanField(_('Display phone number on site'))
      price = TextField(_('Price'))
      password = PasswordField(_('Password'))
      email = TextField(_('Email'))
  </code></pre>
  
  <p>When I try to access the data posted via the form the data turns out as <code>None</code>:</p>
  
  <pre><code>form = AdForm(data=self.request.POST)
  if form.title:
    logging.info('getting title:'+form.title.data)
    ad.title = form.title.data
    ad.save()
  </code></pre>
  
  <p>The above does not save anything to the datastore and this is the template where it's coming from</p>
  
  <pre><code>  &lt;div class="labelform"&gt;
           &lt;div class="labelform" style="clear:left;"&gt;
      &lt;label&gt; {% filter capitalize %}{% trans %}title{% endtrans %}{% endfilter %}:&lt;/label&gt;
    &lt;/div&gt;
    &lt;/div&gt;
    &lt;/td&gt;&lt;td&gt;
  {{ form.title }}{% if form.title.errors %}
          &lt;ul class="errors"&gt;{% for error in form.title.errors %}&lt;li&gt;{{ error }}&lt;/li&gt;{% endfor %}&lt;/ul&gt;
      {% endif %}
  </code></pre>
  
  <p>Can you help me? There's something in the <a href="http://wtforms.simplecodes.com/docs/0.6.1/ext.html#module-wtforms.ext.appengine" rel="nofollow">WTForms manual about appengine</a> but I couldn't find a working example. </p>
  
  <p><strong>Update</strong></p>
  
  <p>I added validation tests and I still can't access the variables:</p>
  
  <pre><code>logging.info('getting requests')
  if form.validate():
    if form.title:
      logging.info('getting title:'+form.title.data)
      ad.title = form.title.data
      ad.save()
      ad.put()
  </code></pre>
  
  <p>Logging output:</p>
  
  <blockquote>
    <p>INFO     2011-11-05 23:17:24,653 main.py:1504] getting requests INFO<br>
    2011-11-05 23:17:24,653 main.py:1507] getting title:</p>
  </blockquote>
  
  <p><strong>Update 2</strong></p>
  
  <p>I removed the WTForms dependence and it is still not working. The line <code>logging.info('getting data:'+ self.request.get('title', '0'))</code> only outputs 0 even though the form is just a regular http post form:</p>
  
  <pre><code> &lt;form action="{{form_url}}" name="upload" method="post" enctype="multipart/form-data" accept-charset="utf-8"&gt;
  </code></pre>
  
  <p><strong>Update 3</strong></p>
  
  <p>This minimal config with no WTForms and no Jinja works so it's probably something with Jinja when this bare-bones example works with webapp2 and python 2.7 where I'm going to add the faulty code line by line to troubleshoot:</p>
  
  <pre><code>class GuestPage(BaseHandler):
      def get(self):
          self.response.out.write("""
            &lt;html&gt;
              &lt;body&gt;
                &lt;form action="/sign" method="post"&gt;
                  &lt;div&gt;&lt;textarea name="content" rows="3" cols="60"&gt;&lt;/textarea&gt;&lt;/div&gt;
                  &lt;div&gt;&lt;input type="submit" value="Sign Guestbook"&gt;&lt;/div&gt;
                &lt;/form&gt;
              &lt;/body&gt;
            &lt;/html&gt;""")
  
  
  class Guestbook(BaseHandler, I18NHandler, blobstore_handlers.BlobstoreUploadHandler):
      csrf_protect = False
  
      def post(self):
          self.response.out.write('&lt;html&gt;&lt;body&gt;You wrote:&lt;pre&gt;')
          self.response.out.write(self.request.get('content'))
          self.response.out.write('&lt;/pre&gt;&lt;/body&gt;&lt;/html&gt;')
  
  
  app = webapp2.WSGIApplication([        ('/guest', GuestPage),
                                        ('/sign', Guestbook),
  </code></pre>
  
  <p>...</p>
  
  <p><strong>Update 4</strong></p>
  
  <p>My back to basics is working with Jinja so I suppose I just build on this example and see where it breaks:</p>
  
  <pre><code>class GuestPage(BaseHandler):
      def get(self):
      self.render_jinja('form_jinja')
  
  class Guestbook(BaseHandler, I18NHandler, blobstore_handlers.BlobstoreUploadHandler):
      csrf_protect = False
  
      def post(self):
          self.response.out.write('&lt;html&gt;&lt;body&gt;You wrote:&lt;pre&gt;')
          self.response.out.write(self.request.get('content'))
          self.response.out.write('&lt;/pre&gt;&lt;/body&gt;&lt;/html&gt;')
  </code></pre>
  
  <p><strong>Update 5</strong></p>
  
  <p>I can reproduce the error with this minimal example that can't access the http post variable:</p>
  
  <pre><code>class GuestPage(webapp2.RequestHandler):
      def get(self):
  
          self.response.out.write("""
            &lt;html&gt;
              &lt;body&gt;
                &lt;form action=" """ +blobstore.create_upload_url('/sign')+ """ " method="post"&gt;
                  &lt;div&gt;&lt;textarea name="content" rows="3" cols="60"&gt;&lt;/textarea&gt;&lt;/div&gt;
                  &lt;div&gt;&lt;input type="submit" value="Sign Guestbook"&gt;&lt;/div&gt;
                &lt;/form&gt;
              &lt;/body&gt;
            &lt;/html&gt;""")
  
  
  class Guestbook(blobstore_handlers.BlobstoreUploadHandler):
  
      def post(self):
          self.response.out.write('&lt;html&gt;&lt;body&gt;You wrote:&lt;pre&gt;')
          self.response.out.write(self.request.get('content'))
          self.response.out.write('&lt;/pre&gt;&lt;/body&gt;&lt;/html&gt;')
  
  
  app = webapp2.WSGIApplication([       ('/guest', GuestPage),
                                        ('/sign', Guestbook),
  </code></pre>
  
  <p><strong>Update 6</strong></p>
  
  <p>From the guestbook example code with blobstoreuploadhandler I can upload a file on the production server so I could make a working example that uses the blobstoreuploadhandler that I will try to build on for my use case.</p>
  
  <p><strong>Update 7</strong></p>
  
  <p>I could get my original code so that everything works except the blob transfer. I suspect a diff between dev_appserver and production that I posted to the google appengine group about. We'll see how it progresses.</p>
  
  <p><strong>Update 8</strong>
  Here's another common use how nothing works when you add WTForms:</p>
  
  <pre><code>    logging.info('getting data:'+ self.request.get('title', '0'))
      logging.info('http post data:'+ str(self.request.post))
      form = AdForm(formdata=self.request.data)
      logging.info('populated form')
      logging.info('form data:' + str(form.formdata))
      if form.validate():
        if form.title:
          logging.info('getting title:'+str( form.get('title') ) )
          ad.title = form.title.data      ad.save()       ad.put()
        if form.text:
          logging.info('getting text:' +str(form.text))
          ad.text = form.text.data
        if self.request.get('currency'):
          ad.currency = self.request.get('currency')
        if self.request.get('cg'):
          ad.category = form.cg.data
        if self.request.get('company_ad') == '1':
          ad.company_ad = True
        ad.put()
      else:
        logging.info('form did not validate')
  except Exception, ex:
      logging.info('there occured exception %s', str(ex))
  </code></pre>
  
  <blockquote>
    <p>INFO     2011-11-09 12:11:50,868 main.py:1385] getting data:TEST INFO 
    2011-11-09 12:11:50,868 main.py:1409] there occured exception post</p>
  </blockquote>
  
  <p><strong>Update 9</strong></p>
  
  <p>Finally the form populates it just doesn't validate. Thank you Sean for the info that got me further. Now I get past populated the form object with no exception but the exception occurs when I try to validate:</p>
  
  <pre><code>logging.info('getting data:'+ self.request.get('title', '0'))
  form = AForm(self.request.POST)
  logging.info('populated form')
  if form.validate():
    logging.info('validated form')
  </code></pre>
  
  <p>The above code is logging the output:</p>
  
  <pre><code>INFO     2011-11-11 08:03:59,913 main.py:1387] getting data:TEST
  INFO     2011-11-11 08:03:59,914 main.py:1390] populated form
  INFO     2011-11-11 08:03:59,914 main.py:1412] there occured exception 'builtin_function_or_method' object is not iterable
  </code></pre>
  
  <p>What does the exception mean?</p>
  
  <p>My form class is</p>
  
  <pre><code>class AForm(Form):
      name = TextField(_('Name'))
      title = TextField(_('title'))
      text = TextAreaField(_('Text'),widget=TextArea())
      phonenumber = TextField(_('Phone number'))
      phonenumberhide = BooleanField(_('Display phone number on site'))
      price = TextField(_('Price'))
      password = PasswordField(_('Password'))
      email = TextField(_('Email'))  
      category  = SelectField(choices=categories.keys)
  </code></pre>