Pergunta

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.

Foi útil?

Solução

i have evaluate your project. your ran into this issue, totally caused by wrong configuration. first,reference here resource. How to load a javascript or css file into a BottlePy template?

you will notice,you can set hash to mapping your static folder

@route('/static/:path#.+#', name='static')
def static(path):
    return static_file(path, root='static')

another issue, we need explicit set the views path in your openshift env.

   from bottle import TEMPLATE_PATH                                               

   import os                                                                      
   views_path = os.environ['APPDIR'] + '/repo/wsgi/views'                         
   TEMPLATE_PATH.insert(0,views_path)                                             
   app = default_app()  

views in template:

<head>                                                                         
<script type="text/javascript" src="{{ get_url('static', path='js/main.js') }}" charset="utf-8"></script>                                                
</head>                                                                        
<body>                                                                         
<p>it's works</p>                                                              
</body>                                                                        
</html>  

Outras dicas

If your bottle can't see the tpl file/s in the views folder.

I used:

TEMPLATE_PATH.append(os.path.join(os.environ['OPENSHIFT_REPO_DIR'], 'wsgi/views/'))

and it works.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top