2012-08-30 23 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 
相关问题