</>
Skip to content
Django lessons (20/43)

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

SettingDescription
STATIC_URLURL prefix
STATICFILES_DIRSAdditional dirs
STATIC_ROOTCollect destination
STATICFILES_FINDERSFinder classes

App Static Files

blog/static/
└── blog/
    └── style.css
{% load static %}
<link rel="stylesheet" href="{% static 'blog/style.css' %}">

Mini Practice

  1. Set up static files directory
  2. Add CSS and JS files
  3. Use static template tag
  4. 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.