2014-10-29 29 views
4

我目前正在使用bottle framework在Python中使用簡單的webapp。這裏是我的應用程序結構:Python瓶框架錯誤500:在守護程序模式下找不到模板

結構

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

這裏是我run.py代碼:

#!/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() 

所以我希望WSGI服務器在後臺運行通過傳遞給bottle daemon script

問題

當運行代碼的非進程化它的工作原理。它向我顯示了正確的模板,並且在CLI中我可以看到HTTP請求。

但是,當我在守護進程模式下運行相同的代碼時,它確實作爲守護進程啓動,因此可以正常工作,但無法再找到該模板。它顯示了我這個錯誤信息:

Error: 500 Internal Server Error

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

Template 'template' not found.

所以它看起來像.tpl文件的文件路徑不能再被發現時,我開始在守護進程的模式的網絡服務器。我已經嘗試了很多東西,但我無法弄清楚,我想保持路徑動態。任何建議傢伙?

謝謝!

回答

6

這可能是一個路徑問題,我能夠重新創建並通過手動將視圖文件夾的路徑添加到瓶TEMPLATE_PATH列表來修復它。

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 

編輯:

它追蹤到問題的根源,這是肯定的路徑問題。 bottledaemon進口daemon並運行DaemonContext默認情況下,它將工作目錄更改爲'/',並且bottledaemon不會像應該那樣覆蓋。因此,當瓶子尋找view文件夾的可靠路徑時,它實際上是在系統的根目錄中查找'/ view'。

+0

回溯(最近通話最後一個): 文件 「run.py」,第6行,在 TEMPLATE_PATH.insert(0,os.path.abspath則(os.path.join(os.path.dirname(__ _文件 _),「view」))) NameError:名稱'TEMPLATE_PATH'未定義 – 2014-10-29 12:58:16

+0

看一下頂部的一行,你需要將它添加到你的導入列表 – CasualDemon 2014-10-29 12:58:50

+0

謝謝!現在守護進程再次啓動。仍然是500錯誤壽。是否有一個靜態路徑,我必須在您提供的代碼中插入? – 2014-10-29 13:03:02