Static folder configuration tells Flask where to find files like images, CSS, and JavaScript that do not change. This helps your web app show styles and pictures correctly.
Static folder configuration in Flask
app = Flask(__name__, static_folder='folder_name', static_url_path='/url_path')
static_folder sets the folder where static files live (default is 'static').
static_url_path sets the URL path to access static files (default is '/static').
app = Flask(__name__)
# Uses default 'static' folder and '/static' URL pathapp = Flask(__name__, static_folder='assets') # Static files are in 'assets' folder but URL path stays '/static'
app = Flask(__name__, static_folder='public', static_url_path='/files') # Static files in 'public' folder, accessed via '/files' URL
This Flask app uses a custom static folder named 'my_static' and serves static files at '/staticfiles'. The HTML page links a CSS file and an image from this folder.
from flask import Flask app = Flask(__name__, static_folder='my_static', static_url_path='/staticfiles') @app.route('/') def home(): return ''' <html> <head> <link rel="stylesheet" href="/staticfiles/style.css"> </head> <body> <h1>Welcome!</h1> <img src="/staticfiles/logo.png" alt="Logo"> </body> </html> ''' if __name__ == '__main__': app.run(debug=True)
Make sure the folder you set for static_folder exists and contains your static files.
The static_url_path is the URL prefix you use in your HTML to access static files.
If you don't set these parameters, Flask uses 'static' folder and '/static' URL by default.
Static folder configuration tells Flask where to find and how to serve static files.
You can customize the folder name and URL path for static files.
This helps organize your files and control how users access images, CSS, and JavaScript.
Practice
Solution
Step 1: Recall Flask default static folder
Flask by default looks for a folder namedstaticin your project to serve static files.Step 2: Confirm other options are not defaults
Folders likeassets,public, orfilesare common in other frameworks but not Flask's default.Final Answer:
static -> Option BQuick Check:
Default static folder = static [OK]
- Confusing 'assets' or 'public' as default static folder
- Thinking Flask serves static files from root directory
- Assuming no default static folder exists
Solution
Step 1: Identify correct Flask constructor parameter
The parameter to change the static folder name isstatic_folder, used asFlask(__name__, static_folder='assets').Step 2: Check other options for errors
static='assets'is invalid, missing__name__is wrong, andstatic_url_pathchanges URL path, not folder name.Final Answer:
app = Flask(__name__, static_folder='assets') -> Option CQuick Check:
Change folder with static_folder param [OK]
- Using static instead of static_folder
- Omitting __name__ argument
- Confusing static_url_path with static_folder
app = Flask(__name__, static_folder='assets', static_url_path='/content') # Assume assets folder contains image.png # What URL path serves image.png?
Solution
Step 1: Understand static_folder and static_url_path roles
static_folder='assets'sets the folder name,static_url_path='/content'sets the URL prefix to access static files.Step 2: Determine URL to access image.png
Since URL path is set to/content, the image is served at/content/image.png.Final Answer:
/content/image.png -> Option AQuick Check:
URL path = static_url_path value [OK]
- Assuming URL matches folder name
- Using default /static URL path despite override
- Confusing folder name with URL path
app = Flask(__name__, static_folder='staticfiles') # staticfiles folder exists with CSS files # Trying to access /static/style.css returns 404
Solution
Step 1: Check static_folder and URL path mismatch
Flask serves static files at URL path/staticby default, but folder is renamed tostaticfiles.Step 2: Understand URL path must match static_url_path
Since static_url_path is not set, Flask expects files at/staticURL but folder isstaticfiles, causing 404.Final Answer:
The URL path /static does not match the static_folder name 'staticfiles' -> Option AQuick Check:
URL path must match static_folder or static_url_path [OK]
- Assuming folder rename auto-updates URL path
- Not setting static_url_path when changing folder
- Believing Flask never serves static files automatically
Solution
Step 1: Set static_folder to 'public_assets'
To serve files from 'public_assets' folder, setstatic_folder='public_assets'.Step 2: Keep URL path as '/static'
Setstatic_url_path='/static'to keep URL path unchanged.Step 3: Verify other options
app = Flask(__name__, static_folder='static', static_url_path='/public_assets') swaps folder and URL path, C uses default URL path '/static', D misses folder setting.Final Answer:
app = Flask(__name__, static_folder='public_assets', static_url_path='/static') -> Option DQuick Check:
Folder and URL path set explicitly [OK]
- Swapping folder and URL path values
- Not setting static_url_path when folder changes
- Assuming default URL path changes with folder
Ready to prove it?
Pass the test and this pattern moves unknown
