<?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>DEV Community: success</title>
    <description>The latest articles on DEV Community by success (@successhycenth).</description>
    <link>https://dev.to/successhycenth</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%2F616308%2F1e0c4cb5-16d4-48bd-932d-e2bf24b25bef.jpg</url>
      <title>DEV Community: success</title>
      <link>https://dev.to/successhycenth</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/successhycenth"/>
    <language>en</language>
    <item>
      <title>Uploading images to Cloudinary Storage from a Django DRF Application.</title>
      <dc:creator>success</dc:creator>
      <pubDate>Mon, 21 Feb 2022 01:17:12 +0000</pubDate>
      <link>https://dev.to/successhycenth/uploading-images-to-cloudinary-storage-from-a-django-drf-application-c40</link>
      <guid>https://dev.to/successhycenth/uploading-images-to-cloudinary-storage-from-a-django-drf-application-c40</guid>
      <description>&lt;p&gt;Sometimes while working on applications that require the use of images, a simple process like uploading images to a server can become difficult. If you deploy your application to a platform like Heroku, you can’t save images.&lt;/p&gt;

&lt;p&gt;Alternatively, we could have our images stored in the database, but the database size will explode over time. Hence, a reason images should be stored in external services like &lt;strong&gt;Cloudinary&lt;/strong&gt;, AWS S3, or Imgur.&lt;/p&gt;

&lt;p&gt;The advantage Cloudinary has is that “configuration is quick and easy”. In this tutorial, we will learn about how to save images in Django using Cloudinary.&lt;/p&gt;

&lt;p&gt;Cloudinary is an end-to-end image and video management solution for websites and mobile apps. It covers everything from image and video uploads, storage, manipulations, and optimizations to delivery.&lt;/p&gt;

&lt;h2&gt;
  
  
  Prerequisites
&lt;/h2&gt;

&lt;p&gt;To follow along with this tutorial, you’ll need Python3 installed on your machine and virtualenv as well.&lt;/p&gt;

&lt;p&gt;A basic understanding of Django would help the reader follow along better.&lt;/p&gt;

&lt;h2&gt;
  
  
  Setting up a new Django project
&lt;/h2&gt;

&lt;p&gt;Let’s start by creating a new virtual environment.&lt;/p&gt;

&lt;p&gt;A virtual environment allows you to create different spaces on your computer, with a different set of libraries and versions.&lt;/p&gt;

&lt;p&gt;By creating a virtual environment, you’ll be able to separate the necessary library installation for a project, without having to install them globally.&lt;/p&gt;

&lt;p&gt;Now, you create a virtual environment env in your preferred folder as shown below:&lt;/p&gt;

&lt;p&gt;to install virtualenv on your machine run&lt;/p&gt;

&lt;p&gt;&lt;code&gt;$ pip3 install virtualenv&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now use your installed virtualenv to create a new virtual environment&lt;/p&gt;

&lt;p&gt;&lt;code&gt;$ virtualenv env&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Here, we need to change directory to our specified project folder where our env is.&lt;/p&gt;

&lt;p&gt;Now you can activate the virtual environment using the following command:&lt;/p&gt;

&lt;p&gt;On Linux or Mac:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;$ source env/bin/activate&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;On Windows:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;$ .\env\Scripts\activate&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;After activation we can install the latest Django on our virtual environment using the following command:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;$ pip3 install django&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now lets create our Imageapp project using the django admin command&lt;/p&gt;

&lt;p&gt;&lt;code&gt;$ django-admin startproject imageapp&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now lets create a django app to hold our models&lt;/p&gt;

&lt;p&gt;&lt;code&gt;$ django-admin startapp images&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Let’s add our app to the list of installed apps. Navigate to our Imageapp directory and edit the settings.py file,&lt;br&gt;
Your settings.py file should look like this&lt;/p&gt;

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

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'images.apps.ImagesConfig',
    ]


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

&lt;/div&gt;
&lt;h2&gt;
  
  
  Setting up a Cloudinary Account
&lt;/h2&gt;

&lt;p&gt;Now, let’s head over to the &lt;a href="https://cloudinary.com" rel="noopener noreferrer"&gt;Cloudinary&lt;/a&gt; website to create a new account.&lt;br&gt;
provide your details and we are good.&lt;/p&gt;
&lt;h2&gt;
  
  
  Installing Django cloudinary Stroage
&lt;/h2&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

pip3 install django-cloudinary-storage
pip3 install cloudinary
pip3 install pillow


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

&lt;/div&gt;

&lt;p&gt;Next, we have to add Cloudinary to the list of installed apps, your settings.py something like this:&lt;/p&gt;

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

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'images.apps.ImagesConfig',
    'cloudinary_storage',
    'cloudinary',
    ]


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

&lt;/div&gt;
&lt;h2&gt;
  
  
  Configuration
&lt;/h2&gt;

&lt;p&gt;To use the Django Cloudinary library, we have to configure our cloud_name, api_key, and api_secret.&lt;/p&gt;

&lt;p&gt;We can find our account-specific configuration credentials on the Dashboard page of the account console as shown below:&lt;/p&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%2F8tukxjuszbh6p8he6cmp.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%2F8tukxjuszbh6p8he6cmp.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Dashboard page cloudinary&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Now add the following config to the bottom of your settings.py file and fill in your credentials gotten from cloudinary in-between the quotes. &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': 'your_api_key',
    'API_SECRET': 'your_api_secret',
}


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

&lt;/div&gt;
&lt;h2&gt;
  
  
  Making Cloudinary our default file storage
&lt;/h2&gt;

&lt;p&gt;Now need to add a default file storage above our CLOUDINARY_STORAGE config and set it to cloudinary as shown bellow:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

MEDIA_URL = '/media/'
DEFAULT_FILE_STORAGE = 'cloudinary_storage.storage.MediaCloudinaryStorage'


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

&lt;/div&gt;
&lt;h2&gt;
  
  
  Creating a Django Model
&lt;/h2&gt;

&lt;p&gt;Migrations are Django’s way of propagating changes you make to your models (adding a field, deleting a model, etc.) into your database schema.&lt;/p&gt;

&lt;p&gt;In the photos directory, edit the models.py file and add the following lines of code to it:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

from django.db import models

class Images(models.Model):
    title = models.CharField(max_length=255) # title of the image
    image = models.ImageField(upload_to='images') # image


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

&lt;/div&gt;

&lt;p&gt;Now, let’s migrate our model to the database by running the commands below:&lt;/p&gt;

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

python3 manage.py makemigrations

python3 manage.py migrate


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

&lt;/div&gt;

&lt;p&gt;A superuser has the permissions to create, edit, update, and delete data in Django admin. We create a superuser by running the command below:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;python3 manage.py createsuperuser&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now let’s register the model of photos in the admin.py file, so we can modify it in the Django admin section.&lt;/p&gt;

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

from django.contrib import admin
from .models import Images

admin.site.register(Images)


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

&lt;/div&gt;

&lt;p&gt;Now, we can log in to the admin page.&lt;/p&gt;

&lt;p&gt;To login to the admin section, go to this link &lt;a href="http://localhost:8000/admin/" rel="noopener noreferrer"&gt;localhost:8000/admin&lt;/a&gt; and log in with our just created superuser details.&lt;/p&gt;

&lt;p&gt;Now add photos as usual and you are good to go... your image will have a reference in your admin site which if u click it will open the image with the link showing cloudinary storage and in your cloudinary media library directory you will also find your uploaded image.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;We have learned how Cloudinary provides a better way to handle media content for our webpage. We have also learned to upload images from a Django DRF apps.&lt;/p&gt;

&lt;p&gt;Happy coding...&lt;/p&gt;

</description>
      <category>django</category>
      <category>djangorestframework</category>
      <category>cloudinary</category>
    </item>
  </channel>
</rss>
