MEDIA files in production — Django

Vaibhav Shukla
4 min readDec 28, 2021
floppy disks and Django media
source: unsplash.com

There are loads and loads of resources, articles, video tutorials on “How to start a Django app from scratch”, but none of them actually teaches you to deploy your Django application on an actual production grade.

Even if they do show the deployment part, they set DEBUG=True which is a BIG NOOOO for production. Like, seriously, they just seem to create videos for the sake of creating videos, (no offense btw).

But no worries, in this article I’m gonna explain not just HOW but WHY do we even need another set of architecture for production when dealing with media files.

What are media files?

In simple words, those static files which are uploaded by users of the website, are called media files. Such as posts on Instagram, videos on YouTube, thumbnail images on Medium, you see, all those are media files.

Why need another mode?

One can argue that why do we even need a new solution to this problem, we can store it like any other static file used in the project. NO! that’s a bad bad bad practice, and that is the one thing they will never teach you in schools, colleges, or in a random YouTube video.

Storing user uploaded files on your own server can be a big pain on your brain as well as your wallet. Plus the performance issues and accessibilities.

Media files must be handled differently in production, we should use cloud storage from third-party companies like Amazon, Google, Microsoft etc.

Some of the many valid reasons to us these services are:

  • Cloud services like AWS, Azure, Google Cloud Storage are PRO at handling big gigantic clusters of data.
  • They back up the data, so we can count on them and be rest assured that our user’s data is safe.
  • It’s easier to use an API from a service than wasting your time in making your own way of handling files.

MEDIA FILES in Django

Here are some of the examples of best cloud storage for production

  • S3 Bucket
  • Azure File Storage
  • Google Cloud Storage
  • Cloudinary (FREE)

I’m personally gonna talk about Cloudinary because I want everyone to follow up, no matter if you’re a college student with 0 bucks or a well-paid software engineer.

Go to settings.py

First, in the INSTALLED_APPS, you have to add these two lines

INSTALLED_APPS = [
...
'cloudinary_storage',
'cloudinary',
]

Second, anywhere in the settings.py just add these configurations

CLOUDINARY_STORAGE = {
'CLOUD_NAME': '<your_cloud_name>',
'API_KEY': '<your_api_key>',
'API_SECRET': '<your_api_secret>',
}

NOTE: If you are very naive and don’t know where to get APIs, you can get these API keys from cloudinary.com, just sign up, NO! this is not sponsored!! I’m just trynna help you.

Finally, just add these variables at the end (or anywhere but the top) of the settings.py

# Media settings
DEFAULT_FILE_STORAGE = 'cloudinary_storage.storage.RawMediaCloudinaryStorage'
MEDIA_URL = '/media/'
MEDIA_ROOT = BASE_DIR / 'media'

BUT NOW LISTEN CAREFULLY, if you’re a student, then I bet you don’t have your credit card and so you can’t create multiple apps in Cloudinary with one account. So here is a trick, instead of making MEDIA_URL = ‘/media/’, you should rather use

MEDIA_URL = '/<your_project_name>/media/'

WHY? It’s tough to explain in words, but you’ll find out when you’ll create more than one Django project with one Cloudinary app.

Basically, it’s to avoid merge of two projects by creating a separate folder for each Django project, since you are using one Cloudinary app for two Django projects. If you’re not getting it… leave it, leave the last step, you’ll eventually figure it out.

Some other top-of-the-hat advices,

  • Set DEBUG=False, the moment you do this, you switched to production mode, and local media files will be no longer in use.
  • Run python manage.py collectstatics --no-input command IF you are dealing with static files in production.
  • Try to compress the files, like images or videos on the fly IF possible, otherwise don’t worry.
  • If facing a Server Error (500) after any of these steps, here is a StackOverflow answer, which I’d asked myself only — https://stackoverflow.com/questions/69105815/django-server-error-500-when-debug-false

Some of these services like AWS and Cloudinary, even provide other great services on top of that. Like image compression, face recognition, theme color recognition, adult image censoring and many more such cool features. So there is a big yes on using it.

Nowadays, it’s even more preferred to store static files on the cloud, which is not necessary but is still counted in best practices. The reason behind this is that your domain server might be slow which might take a lot of time in delivering static files which will ultimately slow down your website performance. So yeah, that’s the whole point.

Of course, there are some other reasons too, for all the points that are made, but in a nutshell, these were the key points that need to be remembered.

You’re actually reading? Wow, thank you.

Many people just copy paste the code and leave 🥲

You can give it a CLAP if you found it helpful

I would appreciate a follow too :)

--

--

Vaibhav Shukla

20 years old Indian Software Developer. IG— @mrvaibh0