django static files
No account is required. Learn the material, try the examples, and mark it complete when you are ready to move on. Sign in with email only if you want to save progress across devices.
Tutorial 08 - Django Static Files
Why this matters
A web application contains more than Python and HTML.
You will use:
CSS
JavaScript
Images
FontsDjango calls these static files.
1. What is a static file?
Examples:
style.css
app.js
logo.png
hero.jpg
favicon.icoThey are generally served as files rather than generated dynamically for every request.
2. A common structure
A Django app may contain:
main/
├── static/
│ └── main/
│ ├── css/
│ │ └── style.css
│ ├── js/
│ │ └── app.js
│ └── images/
│ └── logo.png
├── templates/
│ └── main/
│ └── home.html
├── views.py
└── models.pyThe exact organization can vary.
3. Loading CSS
At the top of a template:
{% load static %}Then:
<link
rel="stylesheet"
href="{% static 'main/css/style.css' %}"
>4. Loading JavaScript
<script
src="{% static 'main/js/app.js' %}">
</script>5. Loading images
<img
src="{% static 'main/images/logo.png' %}"
alt="Company logo"
>6. Why use {% static %}?
You could write a URL manually, but Django's static system provides a consistent way to locate these assets across environments.
The important habit is:
Use Django's static-file mechanism instead of guessing file URLs.
7. When CSS does not load
If:
HTML works
CSS does notcheck:
- Does the CSS file exist?
- Is
{% load static %}present? - Is the path correct?
- Does the browser request the file?
- Is the response successful?
Open:
Developer Tools → NetworkReload the page and find the CSS request.
8. When JavaScript does not work
Check:
- file exists
- path is correct
- script is included
- browser console
- JavaScript execution order
- element IDs/selectors
9. Images
If an image is missing:
image exists
↓
correct static location
↓
correct template path
↓
browser request succeedsDo not immediately replace the image.
First check the path.
10. Development vs production
During development, Django can serve static files in a development setup.
Production requires deliberate static-file configuration.
You will later encounter:
python manage.py collectstaticThis gathers static files for deployment.
11. Browser developer tools
Learn these three areas:
Elements
Network
ConsoleYou do not need the entire browser toolkit.
You need to answer:
Did the browser actually receive the file?
12. Exercise
Add all three to a Django project:
style.css
app.js
logo.pngLoad each from a template.
Then intentionally break one path and use developer tools to diagnose it.
13. Using ChatGPT
Instead of:
CSS doesn't work.
Give:
- project structure
- template
- static-file path
- browser error
Ask:
Trace the static-file path from my template to the CSS file and identify where it breaks.
Remember
HTML
↓
references CSS / JS / images
↓
Django static system
↓
browserWhen a file is missing, inspect the path before rewriting the code.
Finished this lesson?
Keep your place here, or save it across devices with an email link.
Saved in this browser. Save progress with email →