无法在ubuntu上加载烧瓶中的图像

时间:2016-07-08 16:54:34

标签: python flask

我是Flask的新手,我正在尝试做一个项目来学习它。我正在尝试为我的页面加载背景图片,我尝试了很多方法。但是刚收到404错误。 这是我的signup.py文件:

from flask import Flask , render_template
from database_home import Profile
from flask  import Blueprint
sign_up = Blueprint('sign_up' , __name__,template_folder="/home/ali/Desktop/flasklearn/ccs/templates",static_folder="/home/ali/Desktop/flasklearn/ccs/static/css")
@sign_up.route('/')
def Sign_up():
    return render_template('signup.html')

在我的html文件中,我尝试加载背景图片,并确定图像存在:

<body style='background-image:url("../static/img/abstract-wallpaper-widescreen-az-hd-wallpaper-52-stock-64.jpg")'>

我也试过这个:

<body style="background:url {{url_for('sign_up.static',filename='../img/abstract-wallpaper-widescreen-az-hd-wallpaper-52-stock-64.jpg')}}">

还有这个:

<body background="{{url_for('sign_up.static',filename='../images/abstract-wallpaper-widescreen-az-hd-wallpaper-52-stock-64.jpg')}}">

这些都没有奏效。我的项目结构是这样的:

project\
        templates\
        views\
        static\
                css\
                img\

我认为可以加载css文件:

<link href="{{url_for('sign_up.static',filename='main3.css')}}" rel="stylesheet" type="text/css" />

1 个答案:

答案 0 :(得分:0)

以下是您的问题的解决方法:

1 - 在Blueprint定义中,将static指向static文件夹而不是css

sign_up = Blueprint('sign_up' , __name__,
template_folder="/home/ali/Desktop/flasklearn/ccs/templates",
static_folder="/home/ali/Desktop/flasklearn/ccs/static")

2 - 然后uring url_for获取对图像背景的引用:

<body style='background-image:url("{{url_for('sign_up.static',
filename='img/abstract-wallpaper-widescreen-az-hd-wallpaper-52-stock-64.jpg')}}");'>

3 - 至于css文件:

<link href="{{url_for('sign_up.static',filename='css/main3.css')}}"
rel="stylesheet" type="text/css" />