Chapter 6 · 10 min read
Static Files
Static Files
A real website needs more than HTML: stylesheets, JavaScript and images. Django calls these static files because they do not change from request to request. This chapter shows where to put them and how to link them.
What you will learn
- What static files are and where Django looks for them.
- How to load a CSS file with the
statictemplate tag. - What
STATIC_URLdoes. - Why production needs one extra command,
collectstatic.
How static files work in development
Each app may have a folder named static. Just like templates, you repeat the app name inside it so that file names from different apps cannot clash: blog/static/blog/style.css. The django.contrib.staticfiles app, already in INSTALLED_APPS, finds these folders. While DEBUG is on, runserver serves the files at the address given by STATIC_URL, which is 'static/' in a new project. So the file above is available at /static/blog/style.css.
You never write /static/blog/style.css by hand in a template. You use the static tag so that the address can change (for example, when files move to a CDN in production).
Step by step
1. Create a stylesheet
Create blog/static/blog/style.css:
body { font-family: system-ui, sans-serif; max-width: 42rem; margin: 2rem auto; padding: 0 1rem; line-height: 1.6; }
nav { display: flex; gap: 1rem; align-items: center; border-bottom: 1px solid #ddd; padding-bottom: .5rem; }
.inline { display: inline; }
.meta { color: #666; font-size: .9rem; }
.messages { background: #e6ffed; padding: .5rem 1rem; list-style: none; }
2. Load it in the base template
Update blog/templates/blog/base.html. Two things change: the load tag at the very top, and the link element inside head.
{% load static %}
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>{% block title %}My Blog{% endblock %}</title>
<link rel="stylesheet" href="{% static 'blog/style.css' %}">
</head>
<body>
<nav><a href="{% url 'blog:home' %}">Home</a></nav>
<main>{% block content %}{% endblock %}</main>
</body>
</html>
{% load static %}makes thestatictag available in this file. Every template that uses the tag needs its ownload.{% static 'blog/style.css' %}produces/static/blog/style.css.
3. Images
Put an image at blog/static/blog/images/logo.png, then use:
<img src="{% static 'blog/images/logo.png' %}" alt="Site logo">
Settings that matter
| Setting | Meaning |
|---|---|
STATIC_URL | The address prefix for static files. Default 'static/' |
STATICFILES_DIRS | Extra folders with project-wide static files, not belonging to an app |
STATIC_ROOT | The single folder collectstatic copies everything into for production |
Production in one paragraph
runserver serves static files only for convenience while developing. On a real server, a web server or platform serves them. You run python manage.py collectstatic, which copies every app's static files, including the admin's, into the folder named by STATIC_ROOT, and you configure the server to serve that folder. You will do this in the deployment chapter.
Check it works
- Reload the home page: the font changes and the page is centred.
- Open http://127.0.0.1:8000/static/blog/style.css to see the raw CSS.
Exercises with solutions
Exercise 1: change the colours
Make the page background light grey and the link colour dark blue.
Solution. Add to style.css:
body { background: #f4f4f4; }
a { color: #123a7a; }
Hard-refresh the page (Ctrl+Shift+R, or Cmd+Shift+R on macOS) so the browser does not use its cached copy.
Exercise 2: add a logo
Show an image in the navigation.
Solution. Save any small logo.png into blog/static/blog/images/. In base.html put <img src="{% static 'blog/images/logo.png' %}" alt="Logo" height="32"> inside <nav>. If the image does not appear after adding the first file in a new static folder, restart runserver once.
Exercise 3: a project-wide folder
Create a folder static next to manage.py and register it.
Solution. In config/settings.py:
STATICFILES_DIRS = [BASE_DIR / "static"]
Put extra.css inside that folder and link it with {% static 'extra.css' %}. Files here belong to the project, not to one app.
Common mistakes
- Forgetting
{% load static %}. Django reportsInvalid block tag ... 'static'. Did you forget to register or load this tag? - Wrong folder. Put the file in
blog/static/blog/style.css, notblog/static/style.css. - Seeing no change. The browser cached the old file. Hard-refresh.
- Expecting runserver's behaviour in production. Run
collectstaticand configure a server for the files. - Hard-coding
/static/...in templates. Use thestatictag.
Key takeaways
- Static files live in an app's
static/<appname>/folder. - Load them with
{% load static %}and{% static 'path' %}. STATIC_URLis the public address;STATIC_ROOTis the production collection folder.- Production needs
collectstaticand a server that serves the collected files.
Official links
- Managing static files: https://docs.djangoproject.com/en/6.1/howto/static-files/
- Static files in deployment: https://docs.djangoproject.com/en/6.1/howto/static-files/deployment/
- Tutorial part 6: https://docs.djangoproject.com/en/6.1/intro/tutorial06/
