Search code examples
pythonbottle

Bottlepy: template not found


I have server running on apache. I use bottle.py. When I'm going to xxx/getbio, SOMETIMES it returns :

Error: 500 Internal Server Error: Template 'bio' not found.

This error occurs not all time: if I restart apache, it's normalizing for several hours, but happens again. Here is code fragment :

@route('/getbio')
def getBio():
    return template('bio')

Here is file structure :

xxx/
├── views/
│   ├── bio.tpl
└── index.py

And I didin't missed following lines of code:

sys.path.append(os.path.dirname(os.path.abspath(__file__)))
sys.path.append('views')
os.chdir(os.path.dirname(os.path.abspath(__file__)))

Please help me, because I don't have idea how to fix this bug


Solution

  • Add your template location to TEMPLATE_DIR, not to sys.path:

    bottle.TEMPLATE_PATH.insert(0, 'views')
    

    You may find that it's more robust to use the absolute path:

    bottle.TEMPLATE_PATH.insert(0, '/path/to/xxx/views')