如何在Flask模板上设置背景图像? [英] How to set background image on Flask Templates?

查看:204
本文介绍了如何在Flask模板上设置背景图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的背景图片仅适用于具有@ app.route('/')的模板.

My background-image works only for this template that has @app.route('/').

 <header class="intro-header" style="background-image: url('static/img/home.jpg')">

在以下情况下,此方法效果很好:

This works perfectly fine when:

@app.route('/')
def home():
    return render_template('post.html')

一切正常.我明白了:

127.0.0.1 - - [19/Sep/2016 21:07:11] "GET /static/img/home.jpg HTTP/1.1" 304 

但是当我将相同的模板用于:

But when I use same template with:

@app.route('/post/')
def post():
     return render_template('post.html')

我明白了:

127.0.0.1 - - [19/Sep/2016 21:15:23] "GET /post/static/img/home.jpg HTTP/1.1" 404 -                                                                          

背景图像为空白.

推荐答案

这是一个简单的问题可以通过Flask文档来解决

无论如何,您应该在模板中使用以下内容:

Anyway, you should use something like this in your template:

background-image: url({{ url_for('static', filename='img/home.jpg') }})

但是如果您不想使用Flask方法,请使用:

but if you don't want to use Flask methods use :

url('/static/img/home.jpg')

或为您的文件(例如Apache)使用另一个Web服务器而不是flask默认的Web服务器,并通过 http://yoursite/static/img/home.jpg

or use another web server instead of flask default web server for your files like Apache and access via http://yoursite/static/img/home.jpg

这篇关于如何在Flask模板上设置背景图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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