Django have many features but there's also some limitations in django like serving static files in production. Django serve static files in development when DEBUG set to TRUE in settings.py but when its comes to production it need some extra care for the purpose.
If you haven't install python and django you can start from here Installing python and django
What is static files?
Static files are nothing special its just your CSS, Javascript or images files etc which are used in your website.
For Development
When we are in development and "DEBUG=TRUE" in settings it is easy serve statics in website.
projectname/projectname/urls.py
from django.contrib import admin
from django.urls import path
from django.conf import settings # <-- New
from django.conf.urls.static import static # <-- New
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('appname.urls')), # <-- add this
]
# Development Static Serve
# add blow lines
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL,
document_root=settings.STATIC_ROOT)
projectname/projectname/settings.py
...
STATIC_URL = '/static/'
STATICFILES_DIRS = (os.path.join(BASE_DIR, "static"),)
...
NOTE: Above settings only works when "DEBUG=TRUE" in settings.py
For Production
To serve statics in production we use a package called "Whitenoise".
To install whitenoise, use pip command below
Install Whitenoise
pip install whitenoise
After successfully installing whitenoise you need to add it to middleware.
projectname/projectname/settings.py
...
MIDDLEWARE = [
"django.middleware.security.SecurityMiddleware",
"whitenoise.middleware.WhiteNoiseMiddleware", # <-- add this
"django.contrib.sessions.middleware.SessionMiddleware",
"django.middleware.common.CommonMiddleware",
"django.middleware.csrf.CsrfViewMiddleware",
"django.contrib.auth.middleware.AuthenticationMiddleware",
"django.contrib.messages.middleware.MessageMiddleware",
"django.middleware.clickjacking.XFrameOptionsMiddleware",
]
...
Add Compression add caching support in settings.py
projectname/projectname/settings.py
STATICFILES_STORAGE = "whitenoise.storage.CompressedManifestStaticFilesStorage"
Set STATIC_ROOT and STATIC_DIRS in settings.py
projectname/projectname/settings.py
STATIC_URL = "/static/"
STATICFILES_DIRS = (
os.path.join(BASE_DIR, "static"),
)
STATIC_ROOT = os.path.join(BASE_DIR, "staticfiles")
Add whitenoise to INSTALLED_APPS
projectname/projectname/settings.py
...
# Application definition
INSTALLED_APPS = [
"django.contrib.admin",
"django.contrib.auth",
"django.contrib.contenttypes",
"django.contrib.sessions",
"django.contrib.messages",
"django.contrib.staticfiles",
"whitenoise.runserver_nostatic", # <-- add this
"django.contrib.sitemaps",
...
whitenoise setup is almost done, just one thing left.
when we set DEBUG=False whitenoise start serving static but it serve from STATICROOT which we set in settings.py.
To complete setup run command below in your terminal
python manage.py collectstatic
If Promted, enter yes
Your all statics files will be collected in one place.