📖 Scenario: You are preparing a Django web application to be deployed on a production server. Static files like CSS, JavaScript, and images need to be collected into a single folder so the web server can serve them efficiently.
🎯 Goal: Learn how to configure Django settings and run the collectstatic command to gather all static files into the STATIC_ROOT directory for production use.
📋 What You'll Learn
Create a STATIC_ROOT setting in settings.py with the exact path BASE_DIR / 'staticfiles'
Create a STATIC_URL setting with the exact value '/static/'
Run the collectstatic management command to collect static files
Verify that the collected static files are placed inside the staticfiles directory
💡 Why This Matters
🌍 Real World
Web applications need to serve static files efficiently in production. Collecting static files into one folder helps web servers serve them quickly and reliably.
💼 Career
Knowing how to prepare static files for production is essential for Django developers working on real-world web projects and deployments.
Progress0 / 4 steps
1
Set STATIC_URL in settings.py
In your Django project's settings.py file, create a variable called STATIC_URL and set it to the string '/static/'.
Django
Hint
The STATIC_URL tells Django the base URL to serve static files.
2
Set STATIC_ROOT in settings.py
In settings.py, create a variable called STATIC_ROOT and set it to BASE_DIR / 'staticfiles'. Assume BASE_DIR is already defined as the project base directory.
Django
Hint
The STATIC_ROOT is the folder where all static files will be collected for production.
3
Run the collectstatic command
Run the Django management command python manage.py collectstatic in your terminal to collect all static files into the STATIC_ROOT directory.
Django
Hint
This command copies all static files from apps and locations into the staticfiles folder.
4
Verify collected static files in staticfiles folder
Check that the staticfiles directory exists in your project root and contains the collected static files after running collectstatic.
Django
Hint
Use your file explorer or terminal to confirm the staticfiles folder has static assets.
Practice
(1/5)
1. What is the main purpose of running collectstatic in a Django project before deployment?
easy
A. To gather all static files into one folder for easy serving in production
B. To compile Python code into bytecode
C. To migrate database changes
D. To start the Django development server
Solution
Step 1: Understand what collectstatic does
The collectstatic command collects all static files from apps and places them into a single directory.
Step 2: Recognize its role in production
This makes it easier for the web server to serve static files efficiently in production environments.
Final Answer:
To gather all static files into one folder for easy serving in production -> Option A
Quick Check:
collectstatic gathers static files [OK]
Hint: Remember: collectstatic collects static files for production [OK]
Common Mistakes:
Confusing collectstatic with database migrations
Thinking collectstatic runs the server
Assuming collectstatic compiles Python code
2. Which setting must be defined in settings.py to specify where collectstatic will gather static files?
easy
A. STATIC_ROOT
B. MEDIA_ROOT
C. STATIC_URL
D. STATICFILES_DIRS
Solution
Step 1: Identify the setting for static file collection
STATIC_ROOT tells Django the directory where collected static files will be placed.
Step 2: Differentiate from other static settings
STATIC_URL is the URL prefix, MEDIA_ROOT is for user-uploaded files, and STATICFILES_DIRS lists additional static file locations.
Final Answer:
STATIC_ROOT -> Option A
Quick Check:
STATIC_ROOT sets collection folder [OK]
Hint: STATIC_ROOT is the folder for collected static files [OK]
Common Mistakes:
Confusing STATIC_ROOT with STATIC_URL
Using MEDIA_ROOT for static files
Thinking STATICFILES_DIRS is the collection folder