GAE(和WTForms)的HTTP帖子

时间:2011-11-05 20:33:32

标签: python google-app-engine jinja2 wtforms

如果使用blobstoreuploadhandler完成帖子并且最好还使用i18n本地化消息进行验证,我如何在WT帖子中使用WTForms获取变量? 这是我的代码无效:

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'))

当我尝试访问通过表单发布的数据时,数据结果为None

form = AdForm(data=self.request.POST)
if form.title:
  logging.info('getting title:'+form.title.data)
  ad.title = form.title.data
  ad.save()

以上内容并未向数据存储区保存任何内容,而这是来自

的模板
  <div class="labelform">
         <div class="labelform" style="clear:left;">
    <label> {% filter capitalize %}{% trans %}title{% endtrans %}{% endfilter %}:</label>
  </div>
  </div>
  </td><td>
{{ form.title }}{% if form.title.errors %}
        <ul class="errors">{% for error in form.title.errors %}<li>{{ error }}</li>{% endfor %}</ul>
    {% endif %}
你能帮帮我吗? WTForms manual about appengine中有一些东西,但我找不到一个有效的例子。

更新

我添加了验证测试,但我仍然无法访问变量:

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()

记录输出:

  

INFO 2011-11-05 23:17:24,653 main.py:1504]获取请求信息
  2011-11-05 23:17:24,653 main.py:1507]获得冠军:

更新2

我删除了WTForms依赖,它仍然无法正常工作。即使表单只是常规的http帖子形式,行logging.info('getting data:'+ self.request.get('title', '0'))也仅输出0:

 <form action="{{form_url}}" name="upload" method="post" enctype="multipart/form-data" accept-charset="utf-8">

更新3

没有WTForms且没有Jinja的这个最小配置工作所以当这个简单的例子适用于webapp2和python 2.7时,它可能与Jinja有关,我将逐行添加错误代码以进行故障排除:

class GuestPage(BaseHandler):
    def get(self):
        self.response.out.write("""
          <html>
            <body>
              <form action="/sign" method="post">
                <div><textarea name="content" rows="3" cols="60"></textarea></div>
                <div><input type="submit" value="Sign Guestbook"></div>
              </form>
            </body>
          </html>""")


class Guestbook(BaseHandler, I18NHandler, blobstore_handlers.BlobstoreUploadHandler):
    csrf_protect = False

    def post(self):
        self.response.out.write('<html><body>You wrote:<pre>')
        self.response.out.write(self.request.get('content'))
        self.response.out.write('</pre></body></html>')


app = webapp2.WSGIApplication([        ('/guest', GuestPage),
                                      ('/sign', Guestbook),

...

更新4

我回归基础是与Jinja合作所以我想我只是建立在这个例子的基础上,看看它在哪里打破:

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('<html><body>You wrote:<pre>')
        self.response.out.write(self.request.get('content'))
        self.response.out.write('</pre></body></html>')

更新5

我可以使用这个无法访问http post变量的最小示例重现错误:

class GuestPage(webapp2.RequestHandler):
    def get(self):

        self.response.out.write("""
          <html>
            <body>
              <form action=" """ +blobstore.create_upload_url('/sign')+ """ " method="post">
                <div><textarea name="content" rows="3" cols="60"></textarea></div>
                <div><input type="submit" value="Sign Guestbook"></div>
              </form>
            </body>
          </html>""")


class Guestbook(blobstore_handlers.BlobstoreUploadHandler):

    def post(self):
        self.response.out.write('<html><body>You wrote:<pre>')
        self.response.out.write(self.request.get('content'))
        self.response.out.write('</pre></body></html>')


app = webapp2.WSGIApplication([       ('/guest', GuestPage),
                                      ('/sign', Guestbook),

更新6

从guestbook示例代码中使用blobstoreuploadhandler我可以在生产服务器上上传一个文件,这样我就可以创建一个使用blobstoreuploadhandler的工作示例,我会尝试为我的用例构建它。

更新7

我可以获取原始代码,以便除了blob传输之外一切正常。我怀疑dev_appserver与我在google appengine组中发布的产品之间存在差异。我们将看看它是如何发展的。

更新8 这是另一个常见的用法,当您添加WTForms时,没有任何工作:

    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))
  

INFO 2011-11-09 12:11:50,868 main.py:1385]获取数据:TEST INFO   2011-11-09 12:11:50,868 main.py:1409]发生了异常帖子

更新9

最后,表单填充它只是不验证。谢谢Sean让我更进一步的信息。现在我过去填充了表单对象,没有异常,但是当我尝试验证时会发生异常:

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')

上面的代码记录了输出:

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

例外是什么意思?

我的表单类是

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)

2 个答案:

答案 0 :(得分:1)

我对WTForm一无所知,但我猜想像Django一样,你需要在访问数据之前调用验证函数。在这种情况下,它是form.validate()

form = AdForm(formdata=self.request.POST)
if form.validate():
    ad.title = form.title.data

答案 1 :(得分:1)

Daniel实际上不是data = self.request.POST你需要传递给表单而是formdata而不是数据 http://wtforms.simplecodes.com/docs/dev/forms.html#the-form-class

希望这对所有那些像我一样匆匆通过文档的人来说是有用的