Skip to main content

Command Palette

Search for a command to run...

How to Make Preset Cloudinary Transformations Available to Django Template Tags

Published
0 min read

Cloudinary preset (or named) transformations are powerful ways to group together dynamic alterations to your images. For example, you can create a "default" transformation for your website:

DEFAULT = dict(
    format='auto', 
    quality='auto:good', 
    width='auto',
)

The problem is, how do you make that transformation available to all of your {% cloudinary %} template tags? The Cloudinary Django Sample App has a nice solution for this: put the transformations in a template context processor.

# context_processors.py
def cloudinary_transformations(request):
    return dict(
        DEFAULT = dict(
            format='auto', 
            quality='auto:good', 
            width='auto',
        )
    )
# settings.py
TEMPLATES = [
    {
        ...
        'OPTIONS': {
            'context_processors': [
                ...
                'your_app.context_processors.cloudinary_transformations',
            ],
        },
    },
]

Now the transformation is available across all of your templates:

    <div>{% cloudinary image_id1 DEFAULT %}</div>
    ...
    <div>{% cloudinary image_id2 DEFAULT %}</div>

You can put additional "standard" transformations in the context processor, and you can also specify additional alterations in the template tags when needed:

    <div>{% cloudinary image_id1 DEFAULT %}</div>
    ...
    <div>{% cloudinary image_id2 THUMBNAIL gravity='north_west' ... %}</div>