AttributeError:'unicode'对象没有属性'pop'

时间:2012-09-19 20:43:36

标签: python google-app-engine unicode webapp2

我在gae中有这段python代码。

def post(self):
    cases=self.request.get('cases')
    while cases:
        logging.info("cases: %s " % cases)
        case=cases.pop()

生成此日志。

INFO     2012-09-19 20:23:50,690 views.py:674] cases: [u'court1150'] 
ERROR    2012-09-19 20:23:50,690 webapp2.py:1553] 'unicode' object has no attribute 'pop'
Traceback (most recent call last):
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2/webapp2.py", line 1536, in __call__
    rv = self.handle_exception(request, response, e)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2/webapp2.py", line 1530, in __call__
    rv = self.router.dispatch(request, response)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2/webapp2.py", line 1278, in default_dispatcher
    return route.handler_adapter(request, response)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2/webapp2.py", line 1102, in __call__
    return handler.dispatch()
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2/webapp2.py", line 572, in dispatch
    return self.handle_exception(e, self.app.debug)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2/webapp2.py", line 570, in dispatch
    return method(*args, **kwargs)
  File "/Users/brian/googleapps/scheduler/views.py", line 675, in post
    case=cases.pop()
AttributeError: 'unicode' object has no attribute 'pop'

另一方面,交互式控制台中的代码几乎相同。

cases = [u'court1150'] 
while cases:
    case=cases.pop()
    print case
print cases

我没有收到任何错误,以下打印出来。

court1150
[]

为什么我在gae启动器中收到unicode错误,我该如何解决?

1 个答案:

答案 0 :(得分:3)

self.request.get('cases')不返回值列表,只返回一个值。请尝试使用get_all('cases')

def post(self):
    cases=self.request.get_all('cases')
    while cases:
        logging.info("cases: %s " % cases)
        case=cases.pop()

不需要使用while循环,for循环效果更好:

def post(self):
    cases=self.request.get_all('cases')
    logging.info("cases: %s " % cases)
    for case in cases:
        # do something with case.