如何向App Engine NDB提交“unicode float”?

时间:2015-01-02 18:54:16

标签: python google-app-engine unicode

目前我正在建立一个非常基本的电子钱包/钱包"数据库/应用程序。我所做的是(通过表格)捕获3个主要内容:

  • 姓名
  • 电子邮件
  • 首次购买

自动填充其他相关数据......

model.py:

# -*- coding: utf-8 -*-

from google.appengine.ext import ndb

class Purse(ndb.Model):
  name = ndb.StringProperty(indexed=True)
  email = ndb.StringProperty(indexed=True)
  token = ndb.StringProperty(indexed=True)
  password = ndb.StringProperty(indexed=False)
  qr_img = ndb.StringProperty(indexed=False)
  balance = ndb.FloatProperty(indexed=False)
  first_purchase = ndb.FloatProperty(indexed=False)
  activation_date = ndb.DateProperty(indexed=False)
  status = ndb.BooleanProperty(indexed=False)

例如,从 first_purchase 参数中,每次创建新钱包时,我都会填充余额参数(通过百分比方法);在这里您可以了解我的实施基础知识:

new.html(查看):

<!doctype html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>E-wallet OptiVisión</title>
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <!--[if lt IE 9]>
    <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
  <![endif]-->
  <link rel="stylesheet" href="../bower_components/bootstrap/dist/css/bootstrap.min.css">
  <link rel="stylesheet" href="../bower_components/bootstrap/dist/css/bootstrap-theme.min.css">
  <link rel="stylesheet" href="../css/overrides.css">
</head>
<body style="background-color: #f5f5f5; background-image: none; color: #777777;">
<div class="container">
  <div class="row">
    <div id="formfill" class="col-md-5 col-md-offset-3">
    <img id="optiletras" src="../img/optivision-logo-transparent.png" alt="">
      <h1 class="formtext" style="margin-bottom: 15px;">Nuevo Monedero</h1>
      <form method="post" action="/purses/new">
        <input type="text" name="name" class="form-control formboot" placeholder="Nombre"/><br/>
        <input type="text" name="email" class="form-control formboot" placeholder="E-mail"/><br/>
        <div class="input-group formboot">
          <div class="input-group-addon">$</div>
          <input type="number" name="first_purchase" class="form-control" placeholder="Primera Compra (Monto en M.N.)" step="0.01">
          <div class="input-group-addon">MXN</div>
        </div>
        <br />
        <input type="submit" class="btn btn-primary form-control"/>
        <br />
        <br />
      </form>
    </div>
  </div>
</div>
</body>
</html>

controller.py(新方法):

# -*- coding: utf-8 -*-

class New(webapp2.RequestHandler):

    def get(self):
        template = JINJA_ENVIRONMENT.get_template('new.html')
        self.response.write(template.render())

    def post(self):
        self.purse = Purse(name = self.request.get('name'),
                               email = self.request.get('email'),
                               token = tokengen(),
                               password = passgen(),
                               qr_img = qrgen(),
                               first_purchase = self.request.get('first_purchase'),
                               balance = percentage(10, self.request.get('first_purchase'))
                            )
        self.purse.put()
        delivery()
        self.redirect('/purses/index')

extras.py(PERCENTAGE方法):

# -*- coding: utf-8 -*-

# Calcular Porcentajes
def percentage(percent, whole):
  return (float(percent) * float(whole)) / 100.0

我现在遇到的问题是,(当通过表单创建新钱包时),app引擎会抛出下一个错误:

Traceback (most recent call last):
  File "/home/invitado/GoogleAppEngine/GAE-PythonSDK/lib/webapp2-2.5.2/webapp2.py", line 1535, in __call__
    rv = self.handle_exception(request, response, e)
  File "/home/invitado/GoogleAppEngine/GAE-PythonSDK/lib/webapp2-2.5.2/webapp2.py", line 1529, in __call__
    rv = self.router.dispatch(request, response)
  File "/home/invitado/GoogleAppEngine/GAE-PythonSDK/lib/webapp2-2.5.2/webapp2.py", line 1278, in default_dispatcher
    return route.handler_adapter(request, response)
  File "/home/invitado/GoogleAppEngine/GAE-PythonSDK/lib/webapp2-2.5.2/webapp2.py", line 1102, in __call__
    return handler.dispatch()
  File "/home/invitado/GoogleAppEngine/GAE-PythonSDK/lib/webapp2-2.5.2/webapp2.py", line 572, in dispatch
    return self.handle_exception(e, self.app.debug)
  File "/home/invitado/GoogleAppEngine/GAE-PythonSDK/lib/webapp2-2.5.2/webapp2.py", line 570, in dispatch
    return method(*args, **kwargs)
  File "/home/invitado/GoogleAppEngine/GAE-PythonSDK/optivisionmx/controllers/purse.py", line 53, in post
    balance = percentage(10, self.request.get('first_purchase'))
  File "/home/invitado/GoogleAppEngine/GAE-PythonSDK/google/appengine/ext/ndb/model.py", line 2872, in __init__
    self._set_attributes(kwds)
  File "/home/invitado/GoogleAppEngine/GAE-PythonSDK/google/appengine/ext/ndb/model.py", line 2918, in _set_attributes
    prop._set_value(self, value)
  File "/home/invitado/GoogleAppEngine/GAE-PythonSDK/google/appengine/ext/ndb/model.py", line 1108, in _set_value
    value = self._do_validate(value)
  File "/home/invitado/GoogleAppEngine/GAE-PythonSDK/google/appengine/ext/ndb/model.py", line 1055, in _do_validate
    value = self._call_shallow_validation(value)
  File "/home/invitado/GoogleAppEngine/GAE-PythonSDK/google/appengine/ext/ndb/model.py", line 1247, in _call_shallow_validation
    return call(value)
  File "/home/invitado/GoogleAppEngine/GAE-PythonSDK/google/appengine/ext/ndb/model.py", line 1294, in call
    newvalue = method(self, value)
  File "/home/invitado/GoogleAppEngine/GAE-PythonSDK/google/appengine/ext/ndb/model.py", line 1580, in _validate
    (value,))
**BadValueError: Expected float, got u'2999'**

我已经尝试了解决它的所有内容,并且应用引擎在创建新钱包时仍然无法保存。

2 个答案:

答案 0 :(得分:0)

balance = percentage(10, self.request.get('first_purchase'))

request.get返回一个字符串(Unicode,但是,这是一个细节)。我不知道你的percentage函数做了什么,但在我看来,你需要的只是将参数或percentage的返回值或两者都转换为{{1} } S:

float

balance = float(percentage(10, float(self.request.get('first_purchase')))) 的两次调用之一无疑是多余的,但是,如果没有看到float的代码,就无法分辨哪一个! - )

答案 1 :(得分:0)

这是解决方案:

<强> controller.py:

# -*- coding: utf-8 -*-

class New(webapp2.RequestHandler):

    def get(self):
        template = JINJA_ENVIRONMENT.get_template('new.html')
        self.response.write(template.render())

    def post(self):
        self.purse = Purse(name = self.request.get('name'),
                               email = self.request.get('email'),
                               token = tokengen(),
                               password = passgen(),
                               qr_img = qrgen(),
                               first_purchase = float(self.request.get('first_purchase')),
                               balance = percentage(10, self.request.get('first_purchase'))
                            )
        self.purse.put()
        delivery()
        self.redirect('/purses/index')

<强> extras.py:

# -*- coding: utf-8 -*-

# Calcular Porcentajes
def percentage(percent, whole):
  return float((float(percent) * float(whole)) / 100.0)

服务器抱怨的问题是&#34; first_purchase&#34; 参数没有作为其自己的字段提交浮动,最终修复并预先返回余额的浮动。