Static files such as CSS, JavaScript, and images are essential for Django applications. Django provides built-in settings like STATIC_URL and STATICFILES_DIRS to manage these assets efficiently in development and production.
Static files include CSS, JavaScript, images, fonts, and other assets required for frontend rendering. Django uses its staticfiles framework to manage them.
STATIC_URL defines the public URL path for static files.
# URL prefix for static files
STATIC_URL = '/static/'
This setting tells Django where to look for additional static files.
# Additional directories for static files
STATICFILES_DIRS = [
BASE_DIR / "static",
]
# Load static template tag
{% load static %}
<link rel="stylesheet" href="{% static 'css/styles.css' %}">
<img src="{% static 'images/logo.png' %}" alt="Logo">
# Enable staticfiles app
INSTALLED_APPS = [
'django.contrib.staticfiles',
]
# Collect static files
python manage.py collectstatic
Change the settings below to see how Django translates settings and template tags into actual HTML.