Last Updated : 23 Jul, 2025
Django's collectstatic management command is a powerful feature that helps manage static files by gathering them from various locations within a project and placing them into a single directory for deployment. This command is essential for preparing static files for production environments, ensuring that they are easily accessible and efficiently served to end users.
What is the use of Static Files in Django?Static files in Django are the resources delivered to the user's browser without any modification. They play an important role in the presentation and functionality of web applications. Common types of static files include:
The collectstatic management command in Django is designed to handle the aggregation of static files from various locations within a Django project. During development, static files are stored in different app directories and the project's main static directory. The collectstatic command consolidates all these files into a single directory, known as the "static root."
Working of collectstaticDuring Deployment to Production: Before deploying a Django application to a production environment, it’s essential to run the collectstatic command. This process gathers all static files into a single directory (STATIC_ROOT), making it easier to configure a web server or CDN to serve these files efficiently. The collected static files are then served by the web server, allowing for optimized performance and scalability.
Differences Between Development and Production EnvironmentsDevelopment Environment
Production Environment
We are creating a simple view with some content, css file and js file.
Step 1: Create a django appdjango-admin startproject myprojectStep 2: Configure settings.py Python
cd myproject
python manage.py startapp myapp
#...
STATIC_URL = '/static/'
STATIC_ROOT = BASE_DIR / 'staticfiles'
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static')]
#...
Step 3: Create the View, Template, and Static Files
views.py: Here, we used a function and returning a template.html file.
Python
from django.shortcuts import render
def home(request):
return render(request, 'template.html')
template.html: Create a template directory inside myproject subdirectory. Add file 'template.html' in it.
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>My Django Page</title>
<link rel="stylesheet" href="{% static 'css/styles.css' %}">
</head>
<body>
<h1>Hello, Django!</h1>
<p>This is a test page to demonstrate static file handling.</p>
<script src="{% static 'js/scripts.js' %}"></script>
</body>
</html>
styles.css: Create a static directory in main myproject directory. Create 'css/styles.css' and 'js/scripts.js' files in static.
CSS
/* static/css/styles.css */
body {
background-color: #f0f0f0;
font-family: Arial, sans-serif;
}
h1 {
color: #333;
}
scripts.js: A simple javascript code to console log.
JavaScript
// static/js/scripts.js
console.log("JavaScript is working!");
Step 4. Run the Server and Test
In myproject/urls.py, add the view to the URL patterns.
Python
# myproject/urls.py
from django.contrib import admin
from django.urls import path
from . import views # Import the view
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.home), # Map the root URL to the 'home' view
]
Your project hierarchy should look like this:
Run the server using command:
python manage.py runserver
Open http://127.0.0.1:8000/ in your browser to see the page.
Output:
Output of the simple django view 304 status in css and javascript Step 5. Running the collectstatic CommandTo gather all static files into the directory specified by STATIC_ROOT, run the following command:
python manage.py collectstatic
Output:
When you run this command, Django will:
129 static files copied to 'C:\Users\hp\Desktop\Shalini\myproject\staticfiles'.Step 6. Organized Static Files
After running collectstatic, your STATIC_ROOT directory (e.g., staticfiles) will be organized with all collected static files.
Output: Staticfiles folder is being created in the root directory
When we run the server, in the console, in network tab we get status 200 for static files
Network in console ConclusionThe collectstatic command is an important tool in Django for managing static files effectively. It streamlines the process of gathering all static assets, such as CSS, JavaScript, images, and fonts, into a centralized directory, making them ready for efficient serving in production environments. This centralization simplifies the configuration of web servers and CDNs, enhancing the performance and scalability of web applications.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4