无法从webapp.RequestHandler类发送函数参数

时间:2009-11-12 09:56:34

标签: python google-app-engine

将我的应用程序上传到谷歌应用程序引擎并在浏览器中调用它后,我得到了这个错误。

Traceback (most recent call last):
  File "/base/python_lib/versions/1/google/appengine/ext/webapp/__init__.py", line 507, in __call__
    handler.get(*groups)
  File "/base/data/home/apps/bulkloader160by2/1-5.337695659246114067/new_main.py", line 59, in get
    transfer = _transfer_funds(src_key,dest_key,amt)
  File "/base/data/home/apps/bulkloader160by2/1-5.337695659246114067/new_main.py", line 24, in _transfer_funds
    return db.run_in_transaction(_tx)
  File "/base/python_lib/versions/1/google/appengine/api/datastore.py", line 1885, in RunInTransaction
    DEFAULT_TRANSACTION_RETRIES, function, *args, **kwargs)
  File "/base/python_lib/versions/1/google/appengine/api/datastore.py", line 1982, in RunInTransactionCustomRetries
    result = function(*args, **kwargs)
  File "/base/data/home/apps/bulkloader160by2/1-5.337695659246114067/new_main.py", line 11, in _tx
    account = db.get(src_key)
  File "/base/python_lib/versions/1/google/appengine/ext/db/__init__.py", line 1178, in get
    keys, multiple = datastore.NormalizeAndTypeCheckKeys(keys)
  File "/base/python_lib/versions/1/google/appengine/api/datastore.py", line 136, in NormalizeAndTypeCheckKeys
    keys, multiple = NormalizeAndTypeCheck(keys, (basestring, Entity, Key))
  File "/base/python_lib/versions/1/google/appengine/api/datastore.py", line 115, in NormalizeAndTypeCheck
    (types, arg, typename(arg)))
BadArgumentError: Expected an instance or sequence of (<type 'basestring'>, <class 'google.appengine.api.datastore.Entity'>, <class 'google.appengine.api.datastore_types.Key'>); received None (a NoneType).

我试图在我的处理程序的get方法中调用transfer_funds,但是我收到此错误,这是我的main.py文件,

#!/usr/bin/env python

import wsgiref.handlers
from google.appengine.ext import db
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
from models import UserDetails

def _transfer_funds(src_key,dest_key,amt):
  def _tx():
    account = db.get(src_key)
    amount = float(amt)
    if amount <= 100.0:
      if account.balance < amount:
        return None
    account.balance -= amount
    transfer = Transfer(
        parent = account,
        amount = -amount,
        target = db.get(dest_key)
    )
    db.put([account, transfer])
    return transfer
  return db.run_in_transaction(_tx)  

def _roll_forward(transfer):
  def _tx():
    dest_transfer = Transfer.get_by_key_name(str(transfer.key()), parent=transfer.target.key())
    if not dest_transfer:
      dest_transfer = Transfer(
          parent = transfer.target.key(),
          key_name = str(transfer.key()),
          amount = -transfer.amount,
          target = transfer.key().parent(),
          other = transfer)
      account = UserDetails.get(transfer.target.key())
      account.balance -= transfer.amount
      db.put([account, dest_transfer])
      return dest_transfer
  dest_transfer = db.run_in_transaction(_tx)
  transfer.other = dest_transfer
  transfer.put()
  return True

## Model class for Transfers / Transactions
class Transfer(db.Model):
  amount = db.FloatProperty(required=True)
  target = db.ReferenceProperty(reference_class=UserDetails, required=True)
  other = db.SelfReferenceProperty()
  timestamp = db.DateTimeProperty(required=True, auto_now_add=True)

class MyHandler(webapp.RequestHandler):
    def get(self):
        src_username = str(self.request.get('from_username'))
        dest_username = str(self.request.get('to_username'))
        amt = str(self.request.get('amount'))
        src_key = db.GqlQuery('SELECT __key__ FROM UserDetails WHERE user_name = :uname', uname = src_username).get()
        dest_key = db.GqlQuery('SELECT __key__ FROM UserDetails WHERE user_name = :uname', uname = dest_username).get()
        transfer = _transfer_funds(src_key,dest_key,amt)
        progress = _roll_forward(transfer)
        srcUserDetails = UserDetails.gql('WHERE user_name = :uname', uname = src_username).fetch(1)
        destUserDetails = UserDetails.gql('WHERE user_name = :uname', uname = dest_username).fetch(1)
        values = {
            'progress': progress,
            'srcUser': srcUserDetails,
            'destUser': destUserDetails
        }
        self.response.out.write(template.render('transactions.html', values))  
    def post(self):
        self.redirect('/transactions.html')

def main():
    app = webapp.WSGIApplication([
        (r'.*',MyHandler)], debug=True)
    wsgiref.handlers.CGIHandler().run(app)

if __name__ == "__main__":
    main()

我知道GAE会对请求进行负载均衡,但是这也会扩展到函数调用吗?请帮助我理解为什么我会收到此错误。

2 个答案:

答案 0 :(得分:2)

也许在:

account = db.get(src_key)

src_key是无?

答案 1 :(得分:1)

您收到此错误是因为src_key为None,这意味着此声明:

    src_key = db.GqlQuery('SELECT __key__ FROM UserDetails WHERE user_name = :uname', uname = src_username).get()

不匹配任何行。尝试记录该语句的结果和您正在使用的用户名,并确保存在匹配的记录。