Mastering Django Static vs Media Files in Settings.py

Day 7: Don’t Let Broken Images Kill Your App! 🖼️ Mastering Static vs. Media Files Today is Day 7 of my 30-Day Django Mastery journey, and I’m closing out Week 1 by tackling one of the most common stumbling blocks in web development: Managing Assets. In Django, there is a strict "separation of concerns" between files you provide (CSS/JS) and files your users provide (Profile pictures/Resumes). The Key Difference: 1. Static Files (STATIC): These are the assets that make your site look good—your CSS, JavaScript, and Brand Logos. These are part of your source code. 2. Media Files (MEDIA): These are user-generated. Think of a candidate's profile picture or an uploaded PDF. These are dynamic and live outside your code logic. The Pro Configuration: To handle these like an engineer, you need to tell Django exactly where these live in your settings.py. Python # settings.py # For your CSS/JS/Logos STATIC_URL = 'static/' STATICFILES_DIRS = [BASE_DIR / 'static'] # For User Uploads (e.g., Profile Pics) MEDIA_URL = 'media/' MEDIA_ROOT = BASE_DIR / 'media' The "Production" Secret: collectstatic During development, Django’s runserver handles these easily. But in production (AWS, DigitalOcean, etc.), Django is not meant to serve files—it's too slow. We use the python manage.py collectstatic command to gather every single static asset into one folder so a high-performance web server (like Nginx or WhiteNoise) can serve them to users instantly. The Result? A lightning-fast application where images never break, and your backend stays focused on what it does best: processing data. Question for the community: What’s your preferred way to store media files in production? Amazon S3, Cloudinary, or local storage? Let’s talk strategy below! 👇 #Django #Python #WebDevelopment #30DaysOfCode #BackendEngineering #WebPerformance #FullStackDeveloper #CodingLife

  • diagram

To view or add a comment, sign in

Explore content categories