2
votes

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;
}
1
do you get some kind of error message?gittert
Yep, I get a 404 when it looks for the stylesheet. I've updated the question to include this.Max888
try this line instead? : static_url_path='api_files/static')gittert
@gittert adding that line generates this error: ValueError: urls must start with a leading slash. If I add a leading slash static_url_path='/api_files/static' then the app runs but still 404 for the stylesheet.Max888
explanation as how it should work can be found here: stackoverflow.com/questions/16351826/…gittert

1 Answers

4
votes

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()