Django Cloudinary Storage

0.3.0 · active · verified Thu Apr 16

django-cloudinary-storage is a Django package that provides custom storage backends for Cloudinary, allowing both media and static files to be served from Cloudinary. It also includes management commands for file cleanup. The current version is 0.3.0, released with support for Django 2 and Python 3.6+, with infrequent releases focusing on Django version compatibility.

Common errors

Warnings

Install

Imports

Quickstart

To quickly integrate django-cloudinary-storage, add `'cloudinary_storage'` to `INSTALLED_APPS` and configure the Cloudinary credentials. Then, set `DEFAULT_FILE_STORAGE` for media files and `STATICFILES_STORAGE` for static files in your `settings.py`. Ensure your `CLOUDINARY_CLOUD_NAME`, `CLOUDINARY_API_KEY`, and `CLOUDINARY_API_SECRET` are correctly set, ideally via environment variables.

import os

# settings.py

INSTALLED_APPS = [
    # ... other Django apps ...
    'django.contrib.staticfiles',
    'cloudinary_storage',
]

# ... other Django settings (MIDDLEWARE, TEMPLATES, DATABASES, etc.) ...

# Cloudinary Credentials (strongly recommended to use environment variables)
CLOUDINARY_CLOUD_NAME = os.environ.get('CLOUDINARY_CLOUD_NAME', '')
CLOUDINARY_API_KEY = os.environ.get('CLOUDINARY_API_KEY', '')
CLOUDINARY_API_SECRET = os.environ.get('CLOUDINARY_API_SECRET', '')

# Default Storage for Media Files
DEFAULT_FILE_STORAGE = 'cloudinary_storage.storage.MediaCloudinaryStorage'
MEDIA_URL = '/media/' # This is still needed for Django's internal URL handling

# Default Storage for Static Files
STATICFILES_STORAGE = 'cloudinary_storage.storage.StaticCloudinaryStorage'
STATIC_URL = '/static/' # This is still needed for Django's internal URL handling

# Optional: Configure prefixing for URLs on Cloudinary (e.g., to group files)
CLOUDINARY_STORAGE = {
    'MEDIA_TAG': 'media',
    'STATIC_TAG': 'static',
    'MEDIA_DELIVERY_URL_WITH_PREFIX': True,
    'STATIC_DELIVERY_URL_WITH_PREFIX': True,
    'PREFIX': os.environ.get('CLOUDINARY_URL_PREFIX', None), # e.g., 'my_django_project'
}

# Example usage in a model:
# from django.db import models
# class MyPhoto(models.Model):
#     title = models.CharField(max_length=100)
#     image = models.ImageField(upload_to='photos/%Y/%m/')

view raw JSON →