2012-09-19 101 views
-1

我在gae中有一段python代碼。AttributeError:'unicode'對象沒有'pop'屬性

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錯誤,我該如何解決它?

+2

好吧,很顯然這是不相同的,因爲'cases'是不同類型的。 – Hamish

回答

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循環,一個循環的效果要好得多:

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

雖然爲什麼OP會希望'pop'完全逃脫我。爲什麼不只是'在case情況下:logging.info(...)'? –

+0

這個答案似乎並不奏效。 [ERROR 2012-09-19 21:05:30,810 webapp2.py:1553] getlist Traceback(最近一次調用最後一次): AttributeError:getlist' – zerowords

+0

對不起,已更正爲webapp2,其中方法是稱爲'get_all'。 –