烧瓶,显示图片超出静态文件夹

时间:2020-08-25 05:03:19

标签: python flask

我在 template \ about \ index.html

下有一个html页面

是否可以显示位于同一路径的图像-> template \ about \ img.jpg ? 我尝试了很多方法,但是没有成功。

在这种情况下,我想避免使用静态文件夹

谢谢。

2 个答案:

答案 0 :(得分:0)

[假设您的应用程序对象称为app]

@app.route('/imgIFrame')

def imgIframe():
    with open('template/about/img.jpg') as fin:
        data = fin.read()
        ret = make_response(data, 200)
        ret.headers['Content-Type'] = 'img/jpeg'
        return ret

在您的HTML中,您将需要一个由对该方法的调用填充的iframe,并且应该是金色的。

答案 1 :(得分:0)

您可以创建config.py文件

APP_ROOT = os.path.dirname(os.path.abspath(__file__))
APP_YOUR_IMAGE_DIRECTORY= os.path.join(APP_ROOT, 'template\about\')

然后您可以将该路径导入任何地方

from config import APP_YOUR_IMAGE