2012-08-30 25 views
0

index方法是任何TurboGears控制器類的起點。網址Turbogears。編寫控制器方法

  • localhost:8080
  • localhost:8080/
  • localhost:8080/index

中的每一個映射到RootController.index()方法。

我該如何映射localhost:8080localhost:8080/.index()localhost:8080/indexlocalhost:8080/index.html._lookup()

+0

歡迎來到Stack Overflow!我們鼓勵你[研究你的問題](http://stackoverflow.com/questions/how-to-ask)。如果你已經[嘗試了某些東西](http://whathaveyoutried.com/),請將其添加到問題中 - 如果沒有,請先研究並嘗試您的問題,然後再回來。 – 2012-09-28 07:02:00

回答

0

不要在您的控制器內放置任何索引方法,只需使用_lookup根據您想要執行的操作返回正確的控制器。

這將返回 'INDEX 1' 的的http://本地主機:8080的http://本地主機:8080/而返回 '索引2' 的的http://本地主機:8080 /指數http:// localhost:8080/index.html

class IndexController(BaseController): 
    @expose() 
    def index(self, *args, **kw): 
     return 'INDEX2' 

class NoPathController(BaseController): 
    @expose() 
    def index(self, *args, **kw): 
     return 'INDEX1' 

class RootController(BaseController): 
    @expose() 
    def _lookup(self, *remainder, **params): 
     if not remainder: 
      return NoPathController(), remainder 

     return IndexController(), remainder 
相關問題