2015-05-26 82 views
0

這是我的代碼:爲什麼我得到一個KeyError?

import web 
import json 

urls = (
    '/', 'index' 
    '/runs', 'runs' 
) 
app = web.application(urls, globals()) 
class index: 
    def GET(self): 
     render = web.template.render('templates/') 
     return render.index() 

class runs: 
    def GET(self): 
     return "Test" 

if __name__ == "__main__": app.run() 

而且我得到以下錯誤:

Traceback (most recent call last): 
File "/usr/local/lib/python2.7/dist-packages/web/application.py", line 239, in process 
return self.handle() 
File "/usr/local/lib/python2.7/dist-packages/web/application.py", line 230, in handle 
return self._delegate(fn, self.fvars, args) 
File "/usr/local/lib/python2.7/dist-packages/web/application.py", line 419, in _delegate 
cls = fvars[f] 
KeyError: u'index/runs' 

晴人們似乎忘記了實際創建類(在我的情況下運行),或在是否將其導入失敗需要。除了檢查這些東西,我沒有找到任何其他解決方案。

回答

5

你忘了一個逗號:

urls = (
    '/', 'index' 
#    ^
    '/runs', 'runs' 
) 

沒有逗號,Python的地連接了兩個連續的字符串,讓你真正註冊:

urls = (
    '/', 'index/runs', 'runs' 
) 

,你必須在你的globals()字典中沒有這樣的功能。

如果我在逗號中添加代碼,

+1

哦,我的上帝,你不知道我是怎麼長在我的代碼凝視,我只是不明白:(感謝您的幫助 – RandomDisplayName

1

您的代碼有一個錯字:

urls = (
    '/', 'index', # missing comma 
    '/runs', 'runs' 
) 
+0

這不。提供對這個問題的回答,要批評或要求作者澄清,在他們的帖子下面留下評論 –

+2

這與Martijn Pieters同時出現的答案是完全一樣的,缺少的逗號就是答案。 –

相關問題