Django integration

Overview

Cloudinary is a cloud-based service that provides an end-to-end image and video management solution including uploads, storage, manipulations, optimizations and delivery.

With Cloudinary you can easily upload your media assets to the cloud and automatically perform smart image and video manipulations without installing any complex software. All your media resources are then seamlessly delivered through a fast CDN, optimized and using industry best practices. Cloudinary offers comprehensive APIs and administration capabilities and is easy to integrate with new and existing web and mobile applications.

Quick example

Take a look at the following Cloudinary URL that generates the image below:

Simply accessing the above URL told Cloudinary to transform an uploaded image, create a 150x150px thumbnail using face detection based cropping, round the image's corners, add a sepia effect, convert it to a transparent PNG format, add a watermark layer, rotate the image by 10 degrees and ultimately delivered the resulting image through a fast CDN using smart caching techniques.

Python/Django Library Features

Cloudinary provides an open source Python library for further simplifying the integration with your Python and Django applications:

  • Build URLs for transformation & manipulation.
  • Django form and model integration classes.
  • Django template tags for embedding and transforming resources.
  • API wrappers: upload, administration, sprite generation and more.
  • Direct uploading from the browser using a jQuery plugin.

The library was tested with Python 2.5+ and Django 1.4+.

Python capitalization and data type guidelines

When using the Django SDK, keep these guidelines in mind:

  • Parameter names: snake_case. For example: public_id
  • Classes: PascalCase. For example: CloudinaryField
  • Methods: snake_case. For example: add_tag
  • Pass parameter data as: dict or named arguments

Django - Getting started guide

1Installation

Cloudinary's Django integration library is available as an open-source Python code.

You can install Cloudinary's module using either easy_install or pip package management tools:

pip install cloudinary

Include Cloudinary's Python classes in your code:

import cloudinary
import cloudinary.uploader
import cloudinary.api

Initalize Cloudinary tags in your Django templates:

{% load cloudinary %}

2Configuration

Your cloud_name account parameter is required to build URLs for your media assets. api_key and api_secret are further needed to perform secure API calls to Cloudinary (e.g., image and video uploads). See Account and API setup for more details.

Setting the configuration parameters can be done either programmatically in each call to a Cloudinary method or globally using an environment variable or the config method.

You can find your configuration parameters in the dashboard of our Management Console.

Here's an example of setting configuration parameters globally in your Django application:

cloudinary.config( 
  cloud_name = "sample", 
  api_key = "874837483274837", 
  api_secret = "a676b67565c6767a6767d6767f676fe1" 
)

See Configuration Options for more details and additional configuration methods.

3Upload media assets

You can upload images, videos or any other files from your Django server. Uploading is done over HTTPS using a secure protocol based on the api_key and api_secret parameters you provide.

The following command uploads a local file to Cloudinary:

cloudinary.uploader.upload("my_picture.jpg")

Alternatively, you can a specify a local path, a public HTTP URL, an S3 URL, an IO stream or an actual media file's data. For example:

cloudinary.uploader.upload("http://www.example.com/image.jpg")

Each media asset uploaded to Cloudinary is assigned a unique Public ID and is available for immediate delivery and transformation. The upload method returns an associative array with content similar to that shown in the following example:

{
 u'public_id': u'sample',
 u'version': 1312461204,
 u'width': 864
 u'height': 576,
 u'format': u'jpg',
 u'bytes': 120253,       
 u'url': u'https://res.cloudinary.com/demo/image/upload/v1371281596/sample.jpg',
 u'secure_url': u'https://res.cloudinary.com/demo/image/upload/v1371281596/sample.jpg',       
}

As you can see in the following example, with a single call you can define your own Public ID, apply an incoming transformation before storing the resource file in the cloud, generate derived resources eagerly and assign tags to uploaded resources:

cloudinary.uploader.upload(
  request.FILES['file'],      
  public_id = 'sample_id', 
  crop = 'limit',
  width = 2000,
  height = 2000,
  eager = [
    { 'width': 200, 'height': 200, 
      'crop': 'thumb', 'gravity': 'face',
      'radius': 20, 'effect': 'sepia' },
    { 'width': 100, 'height': 150, 
      'crop': 'fit', 'format': 'png' }
  ],                                     
  tags = ['special', 'for_homepage']
)

Many more upload options as well as direct uploading from the browser are detailed here: Django image upload and Django video upload.

4Display and manipulate images and video

Adding images and video to your Django application is quite straightforward. Just use the image/video method of the CloudinaryImage/CloudinaryVideo class. This method generates the full source URL based on the given parameters and adds the image or video to your HTML code:

For example, displaying the uploaded image with the sample public ID, while providing an alternate text:

cloudinary.CloudinaryImage("sample.jpg").image(alt = "Sample Image")
864x576 JPG (Scaled down)

Now, let's say that you wish to display a small thumbnail of this uploaded image. Simply add the transformation instructions to your call. For example, displaying the 'sample' image transformed to fill a 100x150 rectangle:

cloudinary.CloudinaryImage("sample.jpg").image(width = 100, height = 150, crop = 'fill')
100x150 JPG

This is equivalent to:

<img src='https://res.cloudinary.com/demo/image/upload/c_fill,h_150,w_100/sample.jpg' 
     width='100' height='150' />

Alternatively, you can use Cloudinary's Django template tag:

{% cloudinary "sample.jpg" width=100 height=150 crop="fill" %}

Using simple parameters you can perform powerful manipulations. The Python library builds Cloudinary URLs that you can embed in your web and mobile views for dynamically transforming your uploaded resources in the cloud and delivering the results through a fast CDN with advanced caching.

You can easily convert formats, resize, perform face detection-based image cropping, apply effects and filters, append textual layers or watermarks and more.

The following command, for example, embeds a JPG thumbnail of a profile photo fetched from Facebook in real-time, crops it to a circle, applies a sepia effect and delivers it optimized through a CDN:

cloudinary.CloudinaryImage("billclinton.jpg", type = 'facebook').image(
        width = 90, height = 98, 
        crop = 'fill', gravity = 'face',
        radius = 'max', effect = 'sepia')
90x98 JPG

For more information about supported transformations and their usage, refer to Image transformations and Video manipulation and delivery.

For more details about Cloudinary's transformation and manipulation in Django, see Django image manipulation and Django video manipulation.

5Sample projects

To find additional useful code samples and learn how to integrate Cloudinary with your Django applications, take a look at our Sample Projects.

Basic Python sample: Uploading local and remote images to Cloudinary and generating various transformation URLs.

Django Photo Album: A fully working web application that allows you to uploads photos, maintain a database with references to them, list them with their metadata, and display them using various cloud-based transformations. Image uploading is performed both from the server side and directly from the browser using a jQuery plugin.

6What's next

Sign up for a free account if you haven't done so already. Follow the steps above and try Cloudinary out. Finished all steps? That's just an example of what Cloudinary can offer. Here's some additional reading material to help you get the best out of Cloudinary:

Learn more about Django image upload and Django video upload .

Explore powerful Django image manipulation and Django video manipulation features and see our Image transformations and Video manipulation and delivery docs.

Browse additional Django integration topics: configuration, migration, Admin API, etc.

Stay tuned for updates, tips and tutorials: Blog, Twitter, Facebook.