Django — Static Files
Static Files Setup
# settings.py
STATIC_URL = '/static/'
STATICFILES_DIRS = [BASE_DIR / 'static']
STATIC_ROOT = BASE_DIR / 'staticfiles'
Directory Structure
static/
├── css/
│ └── style.css
├── js/
│ └── main.js
└── images/
└── logo.png
Template Tags
{% load static %}
<link rel="stylesheet" href="{% static 'css/style.css' %}">
<script src="{% static 'js/main.js' %}"></script>
<img src="{% static 'images/logo.png' %}" alt="Logo">
collectstatic
python manage.py collectstatic
Static Files Settings
| Setting | Description |
|---|---|
| STATIC_URL | URL prefix |
| STATICFILES_DIRS | Additional dirs |
| STATIC_ROOT | Collect destination |
| STATICFILES_FINDERS | Finder classes |
App Static Files
blog/static/
└── blog/
└── style.css
{% load static %}
<link rel="stylesheet" href="{% static 'blog/style.css' %}">
Mini Practice
- Set up static files directory
- Add CSS and JS files
- Use static template tag
- Run collectstatic
Up Next
Continue with Media Files — handling user uploads.
Related Topics
Frequently Asked Questions about Static Files
What is Static Files in Django?
Static Files is a fundamental concept in Django. This lesson explains it step by step with clear examples, making it easy for beginners to understand.
How do I learn Static Files?
Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Static Files.
Why is Static Files important in Django?
Static Files is essential for Django development. Understanding this concept will help you write better code and solve real-world problems more effectively.