当前位置:   article > 正文

python bottle框架 重定向_Python的web框架bottle静态文件的路径

linux python-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)

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号