0
我想使用webob.static.DirectoryApp
。我只是无法弄清楚如何做到这一点:我想通过webob提供文件夹中的文件
从例如在http://docs.webob.org/en/latest/file-example.html我的路由器的样子:
class Router(object):
def __init__(self, static_path=None):
self.routes = []
self.static_path = static_path if static_path is not None else os.path.join(os.path.dirname(os.path.realpath(__file__)), 'static')
self.static_app = DirectoryApp(self.static_path)
def add_route(self, template, controller, **vars):
if isinstance(controller, basestring):
controller = load_controller(controller)
self.routes.append((re.compile(template_to_regex(template)),
controller,
vars))
def __call__(self, environ, start_response):
req = Request(environ)
for regex, controller, vars in self.routes:
match = regex.match(req.path_info)
if match:
req.urlvars = match.groupdict()
req.urlvars.update(vars)
return controller(environ, start_response)
return exc.HTTPNotFound()(environ, start_response)
要创建应用程序服务:
def create_app():
router = Router()
#router.add_route('/', controller='app.controllers.default:index')
router.add_route('/', controller=default.index)
return router
此服务路线到我添加的控制器罚款。我还加了一个self.static_app
。
我只是不知道如何使用它来从static
-文件夹中提供静态文件!有人可以请赐我吗?
你就不能使用类似router.add_route( '/静态',DirectoryApp(路径))?在调用控制器之前,您可能还必须使用req.path_info_pop()才能获取DirectoryApp – gawel
所需的实际路径!这使我走上正轨! – Asken