How to host media files on Cloudinary for Django projects
Hello, welcome to the devmaesters website. In this post I am going to be showing you how you can setup cloudinary and use it to host Images for your Django project in a production or local environment.
First thing you need to do is to install a Django package called django-cloudinary-storage that helps in connecting your Django project to the cloudinary platform. To do so execute the command below in your terminal after activating your virtual environment (always make sure that your virtual environment is active before installing packages) to install the package.
pip install django-cloudinary-storage
Wait for package to finish installing then add the package to your installed app.
There are two ways to add the package and what you are going to be using it for will determine which one to use.
-
If you are going to use this package for static and/or media files, make sure that
cloudinary_storage
is beforedjango.contrib.staticfiles as shown below
INSTALLED_APPS = [
# ...
'cloudinary_storage',
'django.contrib.staticfiles',
'cloudinary',
# ...
]
-
If you are going to use it only for media files though, it is django.contrib.staticfiles which has to be first as shown below;
INSTALLED_APPS = [
# ...
'django.contrib.staticfiles',
'cloudinary_storage',
'cloudinary',
# ...
]
Confirm that the following configurations are in your projects urls.py file
urlpatterns += static(settings.MEDIA_URL, document_root = settings.MEDIA_ROOT)
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
Next thing you need to do is to setup environment variables for your Django project then create an environment variable called ENVIRONMENT when working within your development environment set the value of environment variable to DEVELOPMENT and during production set it to PRODUCTION. The aim of this is that when testing out our app locally there is no need to be uploading our media files to cloudinary as we can save if on our device.
Next go to your settings.py file and add this below the staticfiles configuration. What this does is that it checks for the current value of the environment variable you created previously and if it's value is PRODUCTION, it makes all the media files you upload from then onwards to be uploaded to cloudinary.
if os.environ["ENVIRONMENT"] == "PRODUCTION":
DEFAULT_FILE_STORAGE = 'cloudinary_storage.storage.MediaCloudinaryStorage'
Next go to the cloudinary website through this link and create a free cloudinary account. After your account have been created go to the dashboard page and copy the cloud name, api key and api secret.
Finally copy and paste this into your project settings.py file;
CLOUDINARY_STORAGE = {
'CLOUD_NAME' : os.environ['CLOUD_NAME'],
'API_KEY' : os.environ['CLOUD_API_KEY'],
'API_SECRET' : os.environ['CLOUD_API_SECRET']
}
Then create the following environment variables;
-
CLOUD_NAME = "<cloud name you copied from your cloudinary dashboard>"
-
CLOUD_API_SECRET = "<api secret you copied from your cloudinary dashboard>"
-
CLOUD_API_KEY = "<api key you copied from your cloudinary dashboard>"
And that's all you need to do to setup cloudinary as a media storage for your Django project.
Conclusion
Through this post I have been able to explain the process of setting up cloudinary as a media storage platform for your Django project. Follow the youtube link below to watch the video of the post and if you have any question you can drop a comment down below and I'll respond as soon as I can.