python - Using self.render() in a StaticFileHandler -


i'm trying extend staticfilehandler in such way can process file requests call self.render(filename, **kwargs) on file serve client. (yes, realize @ point it's no longer static file per se).

here's code i'm trying run:

class mustachefilehandler(tornado.web.staticfilehandler):     def get(self, filename):         self.render(_static_root_ + '/' + path.split('/')[len(path.split('/'))-2], userloginstatus='you logged out!')  # ... class application(tornado.web.application):     def __init__(self, **overrides):          handlers = [(r'/(.*)', mustachefilehandler, {'path' : _static_root_})] # ... 

... _static_root_ variable included in server's config file loaded on startup.

the issue i've got is, whenever try get on file know exists on server, following error:

traceback (most recent call last):   file "/usr/local/lib/python2.7/dist-packages/tornado/web.py", line 1332, in _execute     result = method(*self.path_args, **self.path_kwargs)   file "myfile.py", line 173, in     self.render(_static_root_ + '/' + path.split('/')[len(path.split('/'))-2], userloginstatus='you logged out!')   file "/usr/local/lib/python2.7/dist-packages/tornado/web.py", line 747, in render     self.finish(html)   file "/usr/local/lib/python2.7/dist-packages/tornado/web.py", line 877, in finish     self.set_etag_header()   file "/usr/local/lib/python2.7/dist-packages/tornado/web.py", line 1257, in set_etag_header     etag = self.compute_etag()   file "/usr/local/lib/python2.7/dist-packages/tornado/web.py", line 2185, in compute_etag     version_hash = self._get_cached_version(self.absolute_path) attributeerror: 'mustachefilehandler' object has no attribute 'absolute_path' 

i'm not sure what's causing error or how can handle it.

why using staticfilehandler if response not static? break assumptions built class.

staticfilehandler designed subclassed in limited ways described in its documentation. in particular, subclasses should not override get(), , attempting results in error you're seeing.

if want use tornado template engine kind of preprocessor of files on disk, try overriding both get_content , get_content_size, , making them call self.render_string() (also consider if templates not individually self-contained you'll need change get_content_version take dependencies account). however, requires messy caching avoid rendering template multiple times. it's better either a) render templates on fly ordinary requesthandler. or b) write little script render templates, write them disk, , serve results actual static files.


Popular posts from this blog