0
0
Djangoframework~30 mins

collectstatic for production in Django - Mini Project: Build & Apply

Choose your learning style9 modes available
Collect Static Files for Django Production
📖 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
Need a 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
Need a 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
Need a 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
Need a hint?

Use your file explorer or terminal to confirm the staticfiles folder has static assets.