Skip to main content

A Django storage backend to integrate with Cloudinary for static files and media storage.

Project description

A django storage backend for integrating your static and media asset management with the Cloudinary platform. It applies some of the automatic image optimization capabilities provided by cloudinary. These optimization capabilities can be customized using the CLOUDINARY_STORAGE settings variable.

https://github.com/barakaVictor/django-gamma-cloudinary/workflows/Python%20package/badge.svg?branch=main https://img.shields.io/badge/License-BSD%203--Clause-blue.svg https://app.codacy.com/project/badge/Coverage/46f9e273015842829ba79cff86b9d409 https://app.codacy.com/project/badge/Grade/46f9e273015842829ba79cff86b9d409

Quick start

Installation and setup.

Install the package using pip.

$ pip install django-gamma-cloudinary

In case you use Django ImageField, make sure you have Pillow installed.

$ pip install Pillow

Also, this package has a python-magic dependency which is a simple wrapper around the libmagic C library. If running on Windows platform, be sure to also also install python-magic-bin by running pip install python-magic-bin while on linux (Debian/Ubuntu), be sure to install the libmagic C library by running sudo apt-get install libmagic1

Add “gamma-cloudinary” and “cloudinary” to your INSTALLED_APPS setting like this

INSTALLED_APPS = [
        '...',
        'cloudinary',
        'gamma_cloudinary',
]

Next, you need to add Cloudinary credentials to settings.py

CLOUDINARY_STORAGE = {
        'CLOUD_NAME': 'your_cloud_name',
        'API_KEY': 'your_api_key',
        'API_SECRET': 'your_api_secret'
}

Set the STATIC_ROOT and MEDIA_ROOT as well as STATIC_URL and MEDIA_URL.

MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')

STATIC_URL = '/static/'
MEDIA_URL = '/media/'

This package uses this values of MEDIA_ROOT and STATIC_ROOT to determine where to place your static and media assets on Cloudinary. By default, static and media assets are placed at the top level location of your media library on cloudinary

Set the values for STATICFILES_STORAGE and DEFAULT_FILE_STORAGE settings like so

STATICFILES_STORAGE = 'gamma_cloudinary.storage.StaticCloudinaryStorage'
DEFAULT_FILE_STORAGE = 'gamma_cloudinary.storage.CloudinaryStorage'

And you are all set to begin using the storage backend!!

Usage with static assets

To use this backend to serve static assets, use the StaticCloudinaryStorage class as the STATICFILES_STORAGE in your settings file. The setting should be specified as follows

STATICFILES_STORAGE = 'gamma_cloudinary.storage.StaticCloudinaryStorage'

Afterwards, simply using the django static template tag would suffice to display your static files. However, if you require to apply cloudinary transformations to the static files during render, this package defines a custom template tag name gamma_cl_static. To use the tag follow the steps as outlined below.

load the template tag in the template that requires it like so

{% load gamma_cl_static %}

Use the template tag providing it with the transformation options you desire like so

{% gamma_cl_static 'images/test.png' fetch_format='auto' quality='auto' dpr='auto' width='auto' responsive=True %}

Consult the cloudinary documentation for details about which options are available while applying transformations on stored assets

Usage with media assets

For usage with media assets, ensure that the DEFAULT_FILE_STORAGE backend is set to CloudinaryStorage like so

DEFAULT_FILE_STORAGE = 'gamma_cloudinary.storage.CloudinaryStorage'

After setting this setting, all media uploads will end up in a location characterized by a combination of the MEDIA_ROOT and the value of CLOUDINARY_STORAGE['BASE_STORAGE_LOCATION'] if this setting has been set.

After defining the DEFAULT_FILE_STORAGE, proceed to display media assets using the django default method i.e. Assuming we have a model like this

class TestModel(models.Model):
   image = models.ImageField(upload_to='images')

Then displaying the uploaded image would be as simple as

<img src="{{  test.image.url  }}"/>

However, the above method is less flexible as it does not allow one to specify transformations to be applied to the asset before rendering. To achieve this flexibility, one is required to use the cloudinary_url template tag that comes with the cloudinary package, a dependency of django-gamma-cloudinary. This is done following the steps below.

Load the cloudinary template tags in your templates

{% load cloudinary %}

Use the cloudinary_url tag passing it the name(public_id) of the resource to render. This name is easily retrievable from the name attribute of the django ImageField

<img src="{% cloudinary_url team.image.name fetch_format='auto' quality='auto' dpr='auto' width='auto' responsive=True default_image='placeholder' %}"/>

Settings

Below are the settings utilized by this package with default values

CLOUDINARY_STORAGE = {
   'CLOUD_NAME': None,  # required
   'API_KEY': None,  # required
   'API_SECRET': None,  # required
   'BASE_STORAGE_LOCATION': '/base_storage_location/', #parent folder to keep all media and static assets under in cloudinary media library
   'SECURE': True,
       'DEFAULT_IMAGE_QUALITY': 'auto', # the default cloudinary quality setting for delivering images. Options are:auto;best;good;eco;low.
       'IMAGE_FETCH_FORMAT': 'auto',
}

Additional resources

Additional resources are available at:

Support

You can open an issue through GitHub.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-gamma-cloudinary-0.2.3.tar.gz (1.8 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_gamma_cloudinary-0.2.3-py3-none-any.whl (1.8 MB view details)

Uploaded Python 3

File details

Details for the file django-gamma-cloudinary-0.2.3.tar.gz.

File metadata

  • Download URL: django-gamma-cloudinary-0.2.3.tar.gz
  • Upload date:
  • Size: 1.8 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.1 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.10

File hashes

Hashes for django-gamma-cloudinary-0.2.3.tar.gz
Algorithm Hash digest
SHA256 1b2cd885d31861d6a8521e59bcef1c0e2b0071bfd9a86ea2acced8e48f8e16c1
MD5 accc6ce2dc947620f31a5edf540c87ef
BLAKE2b-256 798aecccff51072aa0e6a166240e6bbd54c4a30787e3be07d4134df217357723

See more details on using hashes here.

File details

Details for the file django_gamma_cloudinary-0.2.3-py3-none-any.whl.

File metadata

  • Download URL: django_gamma_cloudinary-0.2.3-py3-none-any.whl
  • Upload date:
  • Size: 1.8 MB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.1 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.10

File hashes

Hashes for django_gamma_cloudinary-0.2.3-py3-none-any.whl
Algorithm Hash digest
SHA256 99526139e7b7ba5187a006b0afd42f67ae5062b5507a5f407d980adbf0741972
MD5 a5a33952dcc339fa484d7bf11d24e319
BLAKE2b-256 59f27e869bd2320047f5bee4ae36a7f2cb65e09a10470e1154981674fe64085b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page