DEV Community

Cover image for Integrating Cloudinary-Storage with Django πŸ”—πŸ”₯
Shubhajeet Pradhan
Shubhajeet Pradhan

Posted on

Integrating Cloudinary-Storage with Django πŸ”—πŸ”₯

Cloudinary is a cloud-based photo and video managing service that offers picture and video uploading, storage, distribution, and transformation among other services. Django is a well-known Python web-based framework that can be used to create a wide range of online applications.

In this article, I'll explain to you how you can integrate Cloudinary-Storage with Django. This will allow you to use Cloudinary' s features to manage your images and videos in your Django applications.

Prerequisites
Before you begin, you will need the following:

  • A Cloudinary account. You can sign up for a free account at

https://cloudinary.com/

  • A Django project. You can create a new Django project using the following command:


Django-admin startproject project_name


Enter fullscreen mode Exit fullscreen mode

Installing the Cloudinary-Storage for Django
The Cloudinary-Storage is a Django package which provides a Python API for interacting with Cloudinary. Run the below command to install the package of Cloudinary:
pip install Django-Cloudinary-storage

After successful installation of Cloudinary-Storage, you need to import the following in your settings.py file



import cloudinary
import cloudinary.uploader
import cloudinary.api
import cloudinary_storage


Enter fullscreen mode Exit fullscreen mode

Before mentioning your api keys and storage keys, don't forget to mention cloudinary_storage in your INSTALLED APSS. It would appear something like this:



# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles', #important
    'project_name',
    'base.apps.BaseConfig',
    'crispy_forms', #optional
    'django_filters',
    'ckeditor', #optional
    'ckeditor_uploader', #optional
    'storages',
    'cloudinary_storage', #important
]


Enter fullscreen mode Exit fullscreen mode

Configuring Cloudinary in Django
To configure Cloudinary in Django, you will need to add the following settings to your Django project's settings.py file:



CLOUDINARY_STORAGE = {
    'CLOUD_NAME': 'your_cloud_name',
    'API_KEY': 'CLOUDINARY_API',
    'API_SECRET': 'CLOUDINARY_API_SECRET'
}


Enter fullscreen mode Exit fullscreen mode

NOTE: Before publishing/deploying your project, make sure you hide your api secret & keys.

Using Environment Variable
For keeping api secret keys safe, one should always use environment variables. Store your API keys or any secret keys like this:



CLOUDINARY_STORAGE = {
    'CLOUD_NAME': 'your_cloud_name',
    'API_KEY': os.environ.get('CLOUDINARY_API'),
    'API_SECRET': os.environ.get('CLOUDINARY_API_SECRET')
}


Enter fullscreen mode Exit fullscreen mode

environment variables explain

Uploading Images to Cloudinary
Once you have configured Cloudinary in Django, you can upload images to Cloudinary but first, you have to do one last thing and that is to configure your file storage in settings.py in the below-mentioned way:


 = 'cloudinary_storage.storage.MediaCloudinaryStorage'

Enter fullscreen mode Exit fullscreen mode

This will allow you to store images in your Cloudinary cloud storage and if you want to upload any text files/ videos then you can use the following:



cloudinary_storage.storage.VideoMediaCloudinaryStorage #for videos
cloudinary_storage.storage.RawMediaCloudinaryStorage # for text files


Enter fullscreen mode Exit fullscreen mode

ConclusionπŸ“Œ
In this article, I've shown you how to integrate Cloudinary-Storage with Django. This will allow you to use Cloudinary' s features to manage your media files in your web apps. There are other features of Cloudinary-Storage which you can find on django-cloudinary-storage 0.3.0 for more exciting customization.
If you find it useful then please share and show some support πŸ™πŸΌ this post πŸ’™. Happy Blogging 😊

Top comments (2)

Collapse
 
varuntiwari profile image
Varun Tiwari

Very informative πŸ‘

Collapse
 
spymonk12 profile image
spyMonk

Thank you for your support my *friend *🌟