Search code examples
pythonwsgibottle

Python Bottle framework error 500: can't find template in Daemon mode


I'm currently working on a simple webapp in Python using the bottle framework. Here is my app structure:

Structure

lib
    - bottle.py
    - bottledaemon.py
    - lockfile.py
    - __init__.py
view
    - dashboard.tpl
run.py

And here is my run.py code:

#!/usr/bin/env python
from lib.bottle import route, template, run, debug, request, static_file
from lib.bottledaemon import daemon_run

debug(mode=True)

@route('/')
def show_index():

    return template('dashboard')

# If the following line is enabled, the server will start in non-Daemon mode.
#run(host='0.0.0.0', port=80, debug=True)

# If the following lines are enabled, the server will start in Daemon mode.
if __name__ == "__main__":
  daemon_run()

So I want the WSGI server to run in a daemon by passing it to the bottle daemon script.

The problem

When run the code non-daemonized it works. It shows me the correct template and in the CLI I can see the HTTP requests.

When I run the same code in daemonized mode however, it does start as a daemon so that works fine, but it can't find the template anymore. It shows me this error msg:

Error: 500 Internal Server Error

Sorry, the requested URL 'HERE IS MY WEBSITE URL' caused an error:

Template 'template' not found.

So it looks like the file path of the .tpl file can't be found anymore when I start the webserver in daemonized mode. I've tried a lot of things already but I can't figure it out and I would like to keep the path dynamic. Any suggestions guys?

Thanks!


Solution

  • It is probably a path issue, I was able to recreate it and fix it by manually adding the path of the view folder to bottles TEMPLATE_PATH list.

    from bottle import route, template, run, debug, request, static_file, TEMPLATE_PATH
    from bottledaemon import daemon_run
    
    import os
    
    TEMPLATE_PATH.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "view")))
    
    # rest of script
    

    Edit:

    Traced it to the root of the problem, it is a path issue for sure. bottledaemon imports daemon and runs DaemonContext which by default changes the working directory to '/'and bottledaemon does not override that like it should. So when bottle looks for a relitive path of the view folder, it's actually looking for '/view' on the root of the system.