1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > python bottle框架 重定向_Python的web框架bottle静态文件的路径

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

时间:2019-05-21 22:21:49

相关推荐

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)

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。