What is Django static files and media files?
Why Interviewers Ask This
Interviewers use this question to quickly assess whether a candidate has the foundational knowledge required for Django development. It reveals whether you understand the building blocks that more complex concepts rely on.
Answer
Django handles two types of files: Static files: CSS, JavaScript, images that are part of the codebase — versioned and deployed with the application. Configuration: STATIC_URL = "/static/" STATICFILES_DIRS = [BASE_DIR / "static"] # Development static files STATIC_ROOT = BASE_DIR / "staticfiles" # Collected output for production. Development: Django's dev server serves static files automatically. Production: collect all static files: python manage.py collectstatic — copies all static files to STATIC_ROOT. Serve via Nginx, WhiteNoise (pip install whitenoise — serves directly from Django), or CDN. Template usage: {% load static %}<link rel="stylesheet" href="{% static "css/style.css" %}">. Media files: user-uploaded content (profile pictures, document uploads) — not versioned with code, must persist between deployments. Configuration: MEDIA_URL = "/media/" MEDIA_ROOT = BASE_DIR / "media". Development: add URL to urls.py: urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT). Production: serve via Nginx or store on cloud (S3 with django-storages). File upload in model: avatar = models.ImageField(upload_to="avatars/", null=True, blank=True) document = models.FileField(upload_to="docs/%Y/%m/"). Access: user.avatar.url, user.avatar.name, user.avatar.size. django-storages: DEFAULT_FILE_STORAGE = "storages.backends.s3boto3.S3Boto3Storage" — store uploads to S3.
Common Mistake
Rushing to answer is a common mistake. Take two seconds to structure your response: definition → example → trade-off. This structure makes complex Django answers easy to follow.
Previous
What is Django settings and configuration?
Next
What is select_related and prefetch_related in Django?