I am working on an application, hosted on openshift and built on their bottle.py quickstart example (https://github.com/openshift/bottle-openshift-quickstart) but I have had a hard time getting the use of static files correct. I have a local version that works with the server built into bottle but when I transfer what works locally into an openshift application it behaves differently.
in my local main.tpl the following lines are used to indicate which files should be loaded...
<link rel="stylesheet" href="{{ get_url('static', file_name='base.css') }}">
<link rel="stylesheet" href="{{ get_url('static', file_name='skeleton.css') }}">
<link rel="stylesheet" href="{{ get_url('static', file_name='layout.css') }}">
in my local app.py the following code attempts to serve the correct file...
app = default_app()
@route('/')
@view('main.tpl')
def index():
return {'get_url': app.get_url}
@route('/static/<file_name>', name="static")
def send_static(file_name):
if file_name[-3:] == 'css':
return static_file(file_name, root=os.path.join(os.getcwd(), 'static', 'styles'))
The conditional is there because I was trying to figure out a way to keep the /static directory organized without having to write several similar send_static
functions, one that had the root location for css files one that had the root location for js files or whatever.
When the code is used in openshift it no longer works, where the local version inserts a '/static/styles/base.css' into the displayed page, the openshift version only inserts '/static/base.css'. To get the css files to load I've stumbled into the following code.
My main.tpl remains the same...
<link rel="stylesheet" href="{{ get_url('static', file_name='base.css') }}">
<link rel="stylesheet" href="{{ get_url('static', file_name='skeleton.css') }}">
<link rel="stylesheet" href="{{ get_url('static', file_name='layout.css') }}">
But in my openshift app.py I've had to explicitly place the sub-directory into the route
application = default_app()
@route('/')
@view('main.tpl')
def index():
return {'get_url': application.get_url}
@route('/static/styles/<file_name>', name="static")
def send_static(file_name):
if file_name[-3:] == 'css':
root_dir = os.path.join(os.getcwd(), 'static', 'styles')
return static_file(file_name, root=root_dir)
Am I missing something about the openshift server environment that causes the difference? The app is working but I would really like to know a proper way to get css files loaded and displayed.
Thanks.