P
P
PythonLady2019-12-29 14:30:12
css
PythonLady, 2019-12-29 14:30:12

Why can't the css file find the image (flask)?

I got the following project structure:
main.py
templates/index.html
static/style/style.css
______/img/bg.jpg Main.py
code:

from flask import Flask, render_template

app = Flask(__name__)


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


app.run(debug=True)

index.html code:
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href='../static/style/style.css' />
</head>
<body>

</body>
</html>

style.css code:
body {
  background-image: url(../img/bg.jpg) repeat;

}

index.html finds the file with styles, but style.css does not see the picture anymore. Help me please!

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
damilurg, 2019-12-29
@damilurg

You need to correctly set the address to the picture, it looks for the picture in the style folder, and not in the root of the project.
../../../ will help

D
Danil K., 2020-12-30
@Danya_Violet

<link rel="icon" type="image/png" href="/static/images/icons/favicon.ico"/>

then just
body {
  background-image: url(/img/bg.jpg) repeat;
}

R
Roman, 2020-12-31
@skipirich

This way of creating url - '../static/style/style.css' for flask is not quite correct for good, it should be like this
Well, since img is in the style folder, then probably like this background-image: url(./img/bg.jpg) repeat;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question