Add Global Static Files
Creating project-wide CSS, JavaScript, and assets
🌐 What are Global Static Files?
Global static files are CSS, JavaScript, and images shared across your entire Django project. Unlike app-specific files, these are stored at the project level and accessible to all templates and apps.
# settings.py - Configure global static directory
STATICFILES_DIRS = [
BASE_DIR / 'static', # Project-level static folder
]
Global vs App Static Files
Understanding the difference helps you organize files properly. Global files are for site-wide resources like base layouts and common utilities, while app files are specific to individual app functionality.
Global Files
Shared across entire project
static/css/base.css
App Files
Specific to one app
myapp/static/myapp/
Base Styles
Site-wide design system
static/css/theme.css
Utilities
Common JavaScript helpers
static/js/utils.js
🔹 Create Global Static Folder
Set up a project-level static directory structure:
myproject/
├── myproject/
│ ├── settings.py
│ └── urls.py
├── static/ # Global static folder
│ ├── css/
│ │ ├── base.css # Base styles
│ │ └── theme.css # Theme variables
│ ├── js/
│ │ ├── main.js # Main JavaScript
│ │ └── utils.js # Utility functions
│ └── images/
│ ├── logo.png # Site logo
│ └── favicon.ico # Favicon
├── myapp/
│ └── static/
│ └── myapp/ # App-specific files
└── manage.py
🔹 Configure STATICFILES_DIRS
Tell Django where to find global static files:
# settings.py
from pathlib import Path
BASE_DIR = Path(__file__).resolve().parent.parent
# Static files configuration
STATIC_URL = '/static/'
# Where collectstatic gathers files
STATIC_ROOT = BASE_DIR / 'staticfiles'
# Additional directories for global static files
STATICFILES_DIRS = [
BASE_DIR / 'static', # Project-level static folder
]
⚠️ Important:
STATICFILES_DIRS should NOT include STATIC_ROOT. They serve different purposes!
🔹 Create Global CSS File
Add a base stylesheet for your entire site:
/* static/css/base.css */
/* Global styles for entire project */
:root {
--primary-color: #007cba;
--secondary-color: #6c757d;
--success-color: #28a745;
--danger-color: #dc3545;
--font-main: 'Arial', sans-serif;
}
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: var(--font-main);
line-height: 1.6;
color: #333;
background-color: #f8f9fa;
}
.container {
max-width: 1200px;
margin: 0 auto;
padding: 20px;
}
.btn {
display: inline-block;
padding: 10px 20px;
background: var(--primary-color);
color: white;
text-decoration: none;
border-radius: 5px;
border: none;
cursor: pointer;
}
.btn:hover {
opacity: 0.9;
}
🔹 Create Global JavaScript File
Add common JavaScript functionality:
// static/js/main.js
// Global JavaScript for entire project
document.addEventListener('DOMContentLoaded', function() {
console.log('Site loaded successfully!');
// Mobile menu toggle
const menuToggle = document.querySelector('.menu-toggle');
const navMenu = document.querySelector('.nav-menu');
if (menuToggle) {
menuToggle.addEventListener('click', function() {
navMenu.classList.toggle('active');
});
}
// Smooth scroll for anchor links
document.querySelectorAll('a[href^="#"]').forEach(anchor => {
anchor.addEventListener('click', function(e) {
e.preventDefault();
const target = document.querySelector(this.getAttribute('href'));
if (target) {
target.scrollIntoView({ behavior: 'smooth' });
}
});
});
// Auto-dismiss alerts after 5 seconds
const alerts = document.querySelectorAll('.alert');
alerts.forEach(alert => {
setTimeout(() => {
alert.style.display = 'none';
}, 5000);
});
});
🔹 Load Global Files in Base Template
Include global static files in your base template:
<!-- templates/base.html -->
{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{% block title %}My Django Site{% endblock %}</title>
<!-- Global CSS -->
<link rel="stylesheet" href="{% static 'css/base.css' %}">
<!-- Favicon -->
<link rel="icon" href="{% static 'images/favicon.ico' %}">
<!-- Page-specific CSS -->
{% block extra_css %}{% endblock %}
</head>
<body>
<header>
<div class="container">
<img src="{% static 'images/logo.png' %}" alt="Logo" class="logo">
<nav class="nav-menu">
<a href="{% url 'home' %}">Home</a>
<a href="{% url 'about' %}">About</a>
</nav>
</div>
</header>
<main class="container">
{% block content %}{% endblock %}
</main>
<footer>
<div class="container">
<p>© 2025 My Django Site</p>
</div>
</footer>
<!-- Global JavaScript -->
<script src="{% static 'js/main.js' %}"></script>
<!-- Page-specific JavaScript -->
{% block extra_js %}{% endblock %}
</body>
</html>
🔹 Use Global Files in Child Templates
Extend base template and add page-specific styles:
<!-- templates/home.html -->
{% extends 'base.html' %}
{% load static %}
{% block title %}Home - My Django Site{% endblock %}
{% block extra_css %}
<!-- Page-specific CSS -->
<link rel="stylesheet" href="{% static 'css/home.css' %}">
{% endblock %}
{% block content %}
<h1>Welcome to My Site</h1>
<p>This page uses global base.css and page-specific home.css</p>
<button class="btn">Click Me</button>
{% endblock %}
{% block extra_js %}
<!-- Page-specific JavaScript -->
<script src="{% static 'js/home.js' %}"></script>
{% endblock %}
Output:
Welcome to My Site
This page uses global base.css and page-specific home.css
🔹 Best Practices
Follow these guidelines for organizing global static files:
✅ Do:
- Use global files for site-wide styles and scripts
- Keep base.css minimal and focused
- Use CSS variables for consistent theming
- Organize files by type (css/, js/, images/)
- Load global files in base template
❌ Don't:
- Put app-specific files in global static folder
- Duplicate code between global and app files
- Load all files on every page (use blocks)
- Mix global and app files in same directory