【问题标题】:Turbogears. Writing controller methods涡轮齿轮。编写控制器方法
【发布时间】:2012-08-25 17:50:48
【问题描述】:
index 方法是任何 TurboGears 控制器类的起点。每个网址
-
localhost:8080
localhost:8080/
localhost:8080/index
映射到RootController.index() 方法。
如何将localhost:8080 和localhost:8080/ 映射到.index(),但将localhost:8080/index 和localhost:8080/index.html 映射到._lookup()?
【问题讨论】:
标签:
controller
turbogears
【解决方案1】:
不要在控制器中放置任何索引方法,只需使用 _lookup 根据您想要执行的操作返回正确的控制器。
这将为 http://localhost:8080 和 http://localhost:8080/ 返回“INDEX1”,同时为 http 返回“INDEX2” ://localhost:8080/index 和 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