供应带瓶子的img

时间:2017-01-06 14:00:26

标签: python html web bottle

我很难在我的瓶子应用程序中显示图像。

我的文件夹结构是:

project|
    --|views
    --|controllers
    --|static
        --|img
              --myimage
    --|models

在客户端,我已经告诉它放置图像:

 <img href="/static/img/myimage.png" ALT="example annotation" WIDTH=500 HEIGHT=300>

在服务器端我使用过:

@app.route('/static/<filename:path>')
def static(filename):
     return static_file(filename, root='static/')

根据瓶子文档,但我仍然无法加载图像。

1 个答案:

答案 0 :(得分:2)

我通常使用以下

import bottle
from bottle import route, run, template, BaseTemplate, static_file

app = bottle.default_app()
BaseTemplate.defaults['get_url'] = app.get_url  # reference to function

@route('/')
def index():
    return template('mytemplate')


@route('/static/<filename:path>', name='static')
def serve_static(filename):
    return static_file(filename, root='static')

run(host='localhost', port=8080)

然后在mytemplate中我使用:

<img src="{{ get_url('static', filename='img/myimage.png') }}" />