0
0
DjangoConceptBeginner · 3 min read

What is STATIC_ROOT in Django and How to Use It

STATIC_ROOT in Django is the folder path where all static files are collected and stored when you run collectstatic. It is used to gather static files from all apps into one place for easy serving in production.
⚙️

How It Works

Imagine you have many small shops (Django apps) each with their own decorations (static files like images, CSS, and JavaScript). When you want to open a big market (your website), you need to gather all decorations in one big warehouse so they can be easily accessed and served to visitors.

STATIC_ROOT is like that warehouse. When you run the command python manage.py collectstatic, Django copies all static files from each app and other static folders into the STATIC_ROOT directory. This makes it simple for your web server to find and serve these files efficiently in a production environment.

💻

Example

This example shows how to set STATIC_ROOT in your Django settings.py and run the collectstatic command.

python
import os

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
🎯

When to Use

You use STATIC_ROOT when preparing your Django project for production. During development, Django serves static files automatically, but in production, a web server like Nginx or Apache needs a single folder to serve these files efficiently.

For example, before deploying your site, you run python manage.py collectstatic to gather all static files into the STATIC_ROOT folder. Then your web server is configured to serve files from this folder, improving performance and organization.

Key Points

  • STATIC_ROOT is the single folder where all static files are collected for production.
  • It works with the collectstatic command to gather files from all apps.
  • During development, Django serves static files without needing STATIC_ROOT.
  • In production, your web server uses STATIC_ROOT to serve static files efficiently.

Key Takeaways

STATIC_ROOT defines the folder where static files are collected for production.
Run python manage.py collectstatic to gather all static files into STATIC_ROOT.
Use STATIC_ROOT only for production; development uses automatic static file serving.
Configure your web server to serve files from the STATIC_ROOT directory.