Reputation: 3780
I tried following a tutorial which intends to demonstrate how to change the location the static and template folder from being in the root directory. However I cannot get the example to work. The application runs fine but returns a 404 when it looks for the stylesheet "GET /static/style.css HTTP/1.1" 404. So it seems like it can find the template but not the stylesheet.
My hello world example is below. Should I be using root_path, or maybe instance_path or template_folder and static_folder?
api_files
static
style.css
templates
index.html
api.py
api.py from flask import Flask, render_template, url_for
# here we can set a different root path
app = Flask(__name__, root_path='api_files/')
@app.route('/')
def index():
"""Render home page."""
return render_template('index.html') # we can render templates as usual
if __name__ == '__main__':
app.run(debug=True)
index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<link rel= "stylesheet" type= "text/css" href= {{ url_for('static', filename='style.css') }}>
</head>
<body>
hello world
</body>
</html>
style.css
body {
color: red;
}
Upvotes: 4
Views: 7217
Reputation: 1882
I was following this very tutorial and faced the same problem. This Stack Overflow comment is correct -- you have pass an absolute path to root_path
. If you don't, templates
will work -- but the static
content definitely won't.
My files look like this:
# │ hello.py
# │
# └───web-content
# ├───static
# │ style.css
# │
# └───templates
# welcome.html
and hello.py
now looks like:
import os
from flask import Flask, render_template
# important - pass an absolute path to root_path
app = Flask(__name__, root_path=os.path.join(os.getcwd(), 'web-content'))
@app.route('/page/<string:page_name>')
def render_static(page_name):
return render_template(f'{page_name}.html')
if __name__ == "__main__":
app.run()
Upvotes: 6