用瓶子提供 img

Serving an img with bottle

我很难在我的 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/')

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

我一般用下面的

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)

然后在我的模板中使用:

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