2013-03-01 98 views
0

時,我在web.py中出現404錯誤,這是我的代碼和我的問題。當使用/(.+)(新手Q)

import web 

render = web.template.render('templates/') 

urls = (
    '/(.+)', 'index' 
) 

class index: 
     def GET(self, lang): 
      return render.index(lang) 

if __name__=="__main__": 
    app = web.application(urls, globals()) 
    app.run() 

和我的index.html是這個:

$def with (lang) 


$if lang == 'en': 
    I just wanted to say <em>hello</em> 
$elif lang =='es' or lang == '': 
    <em>Hola</em>, mundo! 
$else: 
    página no disponible en este idioma 

的問題是,當我運行此代碼,我得到一個404錯誤。我認爲這個問題可能是URL部分,特別是/(.+)。我想我沒有正確使用它,我想讓它工作,以便我可以使用多個參數。當我使用/(.*)時,它可以工作,但不能用於多個參數,並且文檔說對於超過1個參數,我必須使用+而不是*

事先感謝。

回答

0

你應該研究正則表達式,webpy只匹配路徑並將匹配的組傳遞給控制器​​方法。你可以用?將羣組標記爲可選,所以如果它是空的,那麼它不會被捕獲,並且默認情況下lang將被設置爲無。

另外. in regexp表示任何符號,爲了捕獲語言,你最好使用\w匹配任何單詞字符。

urls = (
    '/(\w+)?', 'index' 
) 

class index: 
    def GET(self, lang=None): 
     return render.index(lang)