<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Shubhajeet Pradhan</title>
    <description>The latest articles on Forem by Shubhajeet Pradhan (@spymonk).</description>
    <link>https://forem.com/spymonk</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F946083%2Fe0730106-caac-4b83-927a-664e731bec98.png</url>
      <title>Forem: Shubhajeet Pradhan</title>
      <link>https://forem.com/spymonk</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/spymonk"/>
    <language>en</language>
    <item>
      <title>Integrating Cloudinary-Storage with Django 🔗🔥</title>
      <dc:creator>Shubhajeet Pradhan</dc:creator>
      <pubDate>Thu, 18 May 2023 11:09:00 +0000</pubDate>
      <link>https://forem.com/spymonk/integrating-cloudinary-storage-with-django-4ipb</link>
      <guid>https://forem.com/spymonk/integrating-cloudinary-storage-with-django-4ipb</guid>
      <description>&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Prerequisites&lt;/strong&gt;&lt;br&gt;
Before you begin, you will need the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A Cloudinary account. You can sign up for a free account at &lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;a href="https://cloudinary.com/" rel="noopener noreferrer"&gt;https://cloudinary.com/&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;A Django project. You can create a new Django project using the following command:&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

Django-admin startproject project_name


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Installing the Cloudinary-Storage for Django&lt;/strong&gt;&lt;br&gt;
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:&lt;br&gt;
&lt;code&gt;pip install Django-Cloudinary-storage&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;After successful installation of Cloudinary-Storage, you need to import the following in your &lt;code&gt;settings.py&lt;/code&gt; file&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

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


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Before mentioning your &lt;code&gt;api keys and storage keys&lt;/code&gt;, don't forget to mention &lt;code&gt;cloudinary_storage&lt;/code&gt; in your &lt;strong&gt;INSTALLED APSS&lt;/strong&gt;. It would appear something like this:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

# 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
]


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Configuring Cloudinary in Django&lt;/strong&gt;&lt;br&gt;
To configure Cloudinary in Django, you will need to add the following settings to your Django project's &lt;code&gt;settings.py&lt;/code&gt; file:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

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


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;&lt;u&gt;NOTE&lt;/u&gt;&lt;/strong&gt;: Before publishing/deploying your project, make sure you hide your api secret &amp;amp; keys.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Using Environment Variable&lt;/strong&gt;&lt;br&gt;
For keeping api secret keys safe, one should always use environment variables. Store your API keys or any secret keys like this:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

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


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8a3ocyr78fjqay567plu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8a3ocyr78fjqay567plu.png" alt="environment variables explain"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Uploading Images to Cloudinary&lt;/strong&gt;&lt;br&gt;
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 &lt;code&gt;settings.py&lt;/code&gt; in the below-mentioned way:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
 = 'cloudinary_storage.storage.MediaCloudinaryStorage'

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;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:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

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


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Conclusion📌&lt;/strong&gt;&lt;br&gt;
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 &lt;a href="https://pypi.org/project/django-cloudinary-storage/#usage-with-static-files" rel="noopener noreferrer"&gt;django-cloudinary-storage 0.3.0&lt;/a&gt; for more exciting customization. &lt;br&gt;
If you find it useful then please share and show some support 🙏🏼 this post 💙. Happy Blogging 😊&lt;/p&gt;

</description>
      <category>django</category>
      <category>webdev</category>
      <category>python</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
