python bottle框架 重定向_Python的web框架bottle静态文件的路径
這幾天想用bottle來做一個簡單的基于web頁面的小應用,在調用顯示靜態文件時被路徑卡了半天,現在把問題和解決辦法寫出來備用和分享給有需要的人。
先上代碼:
from bottle import static_file,route,run,TEMPLATE_PATH
TEMPLATE_PATH.insert(0,'./testdir/')
@route('/')
def hello():
return "this is bottle web test!"
@route('/testdir/')
def send_image(filename):
return static_file(filename, root='./testdir/')
run(host='localhost',port='80',debug=True)
說明:在調用testdir目錄中的靜態文件時,root后面的路徑是絕對路徑,剛開始我一直用相對路徑,所以一直出錯。有人會說,現在你的也是相對路徑,是的,我在上面作了處理,加了:TEMPLATE_PATH.insert(0,'./testdir/')這行代碼就可以在root中用相對路徑了。不然只能用:
root='C:/XX/XX/testdir/' 這種格式的絕對路徑了。
還有可用這樣使用:
import os
from bottle import static_file,route,run
#定義一個變量
testdirpath= os.path.abspath(os.path.join(dirname(__file__), "testdir/")) #轉化為絕對路徑
@route('/')
def hello():
return "this is bottle web test!"
@route('/testdir/')
def send_image(filename):
return static_file(filename, root=testdirpath)
run(host='localhost',port='80',debug=True)
總結
以上是生活随笔為你收集整理的python bottle框架 重定向_Python的web框架bottle静态文件的路径的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C++模板(template)中type
- 下一篇: python websocket爬虫_详