1

I have started to work on flask, and the problem is i have created a css file in static folder and wanted to load that css file on template.

but it is not loading css file even if i try this on incognito mode i thought it could be a cache problem but it does not.

<!DOCTYPE html>
<title> Welcome to My Blog</title>

<link rel="stylesheet" type="=text/css" href="{{ url_for('static', filename='style.css?v=0.01') }}">

<h2>Hey there {{ name }}</h2> 

Image :Hello world flask app

Blog.py

from flask import Flask, request, render_template

app = Flask(__name__)


...
@app.route('/profile/<username>')
def profile(username):
    return render_template("profile.html", name=username)

...

if __name__ == '__main__':
    app.run(debug=True, port=8080)

style.css

h2{
    color: #00bfff;
}
0

3 Answers 3

6

The problem is that you include the query string in your filename.

url_for('static', filename='style.css?v=0.01')

The url path results in static/style.css%3Fv%3D0.01, because the special characters get encoded. To fix this you must add query elements as keyword arguments:

url_for('static', filename='style.css', v=0.01)

Additionally:

  • Your file is called stye.css and not style.css
  • Your <link> tag says type="=text/css" and not type="text/css" as it should
Sign up to request clarification or add additional context in comments.

7 Comments

i have changed it but still its the same.
when i try to open; 127.0.0.1:8080/static/style.css?v=0.01 i get a not found error.
i have renamed and rerun but still getting error. i have added style.css file on question.
thank you, i got my lesson, should be more carefull about typo and control every letter before asking question :).
You're welcome! To be honest I was a bit surprised Chrome didn't catch the typo and ignore it. :)
|
0

Adding to the accepted answer, Sometimes encoding itself causes the problems. Make sure you have right encoding.

<meta charset="UTF-8">

Include charset attribute in your HTML file. Also, to avoid using cached responses of your browser, try refreshing your content using ctrl + F5 keys.

Comments

0

In my case I have added following line to my code and it worked

app.static_folder = 'static'

and you have to change the

<link rel="stylesheet" type="=text/css" href="{{ url_for('static', filename='style.css?v=0.01') }}">

to

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.