Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

404 error for CSS file in Flask Application

I am unable to find exact path of .css file in my flask app. Following is relevant code

layout.html

<!DOCTYPE html>
<html>
<head>
    <title>An App</title>
    <link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}">
</head>
<body>
    <div id="container">
        <div id="heading"><h3>App</h3></div>
        {% block content %}{% endblock %}
    </div>
</body>
</html>


+app
  +static
    style.css
+templates
__init__.py
forms.py
models.py
views.py
db_repository
.gitignore
app.db
config.py
db_create.py

The one with + sign are folders

Update:

I tried this in __init__.py, same result

from flask import Flask
from flask.ext.sqlalchemy import SQLAlchemy
import os
app = Flask(__name__)
app.config.from_object('config')
app._static_folder = os.path.abspath("static/style.css")
print os.path.abspath(app._static_folder)
db = SQLAlchemy(app)

from app import views, models

The link http://127.0.0.1:5000/static/style.css gives 404 error

like image 400
Volatil3 Avatar asked Dec 01 '22 18:12

Volatil3


2 Answers

Static should be on the same level as templates, not under app. Have you tried that?

like image 117
minboost Avatar answered Dec 04 '22 15:12

minboost


I you want to change the route to the static asset's folder you could do:

app = Flask(__name__, static_folder='app/static')

check this here

like image 39
Xavi Avatar answered Dec 04 '22 16:12

Xavi