Static Files (Flask)

Static files in Flask are unchanging assets like CSS, JavaScript, and images, served from the static/ folder. You link to them in templates with url_for('static', filename='...') so the correct URL is generated automatically.

Flask

Flask automatically serves any file you place in a folder named static/ next to your app, at URLs under /static/. So static/css/style.css is reachable at /static/css/style.css without you writing a route for it.

Always reference static assets with url_for('static', filename='css/style.css') rather than hard-coding /static/..., so links keep working if you change the static URL path or deploy under a subpath.

Flask's static serving is convenient for development, but in production you typically let nginx or a CDN serve static files for speed, leaving the application server to handle dynamic requests only.

Syntax

url_for("static", filename="css/style.css")

Examples

Python

<!-- in a template -->
<link rel="stylesheet"
      href="{{ url_for('static', filename='css/style.css') }}">
<img src="{{ url_for('static', filename='img/logo.png') }}">

Learn it in a lesson

See also

Back to the Dictionary