从瓶子模板中引用图像的方法是什么? [英] What is the way to reference an image from within a bottle template?

查看:74
本文介绍了从瓶子模板中引用图像的方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在瓶子模板文件中时,引用静态文件的方式是什么?例如这个?

When within a bottle template file, what is the way to reference a static file? for example this one?

<body background='/static/img/digital.gif'>

为什么上面的相对文件无法加载图像?
如何正确引用?如果我将其尝试为:

Why the above relative file fails to load the image? How can i reference it correctly? If i try it as:

<body background="{{ static_file( 'digital.gif', root='./static/img' }}">

图像也无法渲染。

即使不使用'static_file'功能,为何Bottle仍无法渲染图像?

Why Bottle fail to render the image even if no 'static_file' function is used?

推荐答案

您应该在应用程序中添加一条路由,该路由将在请求静态文件时返回静态文件,然后将html中的路径用作常规路径。

You should add a route to your application that will return static files when they are requested. Then use the paths in html as normal paths.

因此添加以下内容:

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

然后访问: http:// localhost:8080 / static / file.txt 将返回'/path/to/static/file.txt'。

then accessing: http://localhost:8080/static/file.txt will return '/path/to/static/file.txt'.

/ path / to / static的子文件夹也将可用。

Sub-folders of '/path/to/static' will also be accessible.

这篇关于从瓶子模板中引用图像的方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆