Skip to main content

django-cloudflare-images

This is a Django library to add support to Cloudflare Images to the ImageField.

It supports:

  • Django 3
  • Django 4
  • Django 5
  • Django 6

Installation

pip install django-cloudflare-images

Setup

You need to add the following your settings.py:

  1. If you are running Django 4.2+:
STORAGES = {"default": {"BACKEND": "cloudflare_images.storage.CloudflareImagesStorage"}}
  1. Else:
DEFAULT_FILE_STORAGE = "cloudflare_images.storage.CloudflareImagesStorage"

And then add the remaining of the configuration:

CLOUDFLARE_IMAGES_ACCOUNT_ID = "XXX"
CLOUDFLARE_IMAGES_API_TOKEN = "YYY"
CLOUDFLARE_IMAGES_ACCOUNT_HASH = "ZZZ"

If you wish to use a default variant for a specific field you need to change your ImageField to a CloudflareImagesField see example below:

from cloudflare_images.field import CloudflareImagesField
from django.db import models


class MyModel(models.Model):
    image = CloudflareImagesField(variant="custom")

Please note that you will need to migrate your model(s) once you swapped the field(s). No SQL will actually be applied (you can check by running sqlmigrate <module> <number>).

If you wish to use a custom domain to serve your images you need to add the following to your settings.py:

CLOUDFLARE_IMAGES_DOMAIN = "example.com"

If you wish to use a default variant other than "public" to serve your images you need to add the following to your settings.py:

CLOUDFLARE_IMAGES_VARIANT = "custom"

If you wish to override the default timeout of 60 seconds for API requests, you need to add the following to your settings.py:

CLOUDFLARE_IMAGES_API_TIMEOUT = 120

Checking if an Image Exists

You can check if an image exists in Cloudflare Images using the storage's exists() method:

from django.core.files.storage import default_storage

if default_storage.exists("image_id"):
    print("Image exists!")
else:
    print("Image not found")

Note: This method makes an HTTP API call to Cloudflare on every invocation. If you need to check many images, consider implementing your own caching layer to reduce API calls.

Direct Creator Upload

If you want to leverage Cloudflare's Direct Creator Upload (client side upload, similar to S3/Minio presigned URL concept), you need to do the following:

Modify your settings:

INSTALLED_APPS = [
    "cloudflare_images",
]

Modify your urls:

from cloudflare_images.views import WidgetAPI

# And add inside your router:
path("ext/cloudflare_images/api", WidgetAPI.as_view(), name="widget-api"),

Please note that it is much safer to implement your own endpoint to retrieve the one time URL from cloudflare

You can override the endpoint to use like so:

CLOUDFLARE_IMAGES_UPLOAD_ENDPOINT = "/my/endpoint/api"

Please refer to this file for the base implementation details

Modify/Add to your model:

from cloudflare_images.field import CloudflareImageIDField

# in your model:
image = CloudflareImageIDField(variant="test")

If you are changing from a CloudflareImagesField, please note that you need to generate a migration (python manage.py makemigrations) and that this new field is not backward compatible in particular to access the .url field. To access your field's URL it is recommended to write a helper (or use a custom mixin) in your model like so:

from cloudflare_images.service import CloudflareImagesService

    @property
    def image_url(self) -> str | None:
        if not self.image:
            return None
        service = CloudflareImagesService()
        return service.get_url(self.image, "public") # or any variant that you want

Modify your HTML template using a django form to load the JS/CSS:

{{ form.media.js }}
{{ form.media.css }}

And you should be good to go. For basic troubleshooting, check your browser's console for hints or open a ticket.

Development

Installing for development:

make install

Cleaning the installation:

make clean

Format the code:

make format

Check the code (for linting errors):

make check

Check the code (python type checker):

make static-check

Running all tests:

make test

Create a sdist+bdist package in dist/:

make package

Release files for django-cloudflare-images 1.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-cloudflare-images 1.1.0
File Size Uploaded
django_cloudflare_images-1.1.0.tar.gz 13.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-cloudflare-images 1.1.0
File Interpreter ABI Platform
django_cloudflare_images-1.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 24.7 kB

Release files / django_cloudflare_images-1.1.0.tar.gz

Download URL django_cloudflare_images-1.1.0.tar.gz
Size 13.8 kB
Tags Source
SHA-256 checksum
How to use checksums
745abaad405f2b4509ca0a0c47c1d993d4cebdafc1057ed845f13459b98c8fef
BLAKE2b-256 checksum
How to use checksums
903c8c76d286fbfae811a51a057953683e2c809fc8959af30314a370ea101e56
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.6

Release files / django_cloudflare_images-1.1.0-py3-none-any.whl

Download URL django_cloudflare_images-1.1.0-py3-none-any.whl
Size 10.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c1773025ce29f7f9f2ca7ccd1982dcbbabcc041afcd097f28fa80b9664601387
BLAKE2b-256 checksum
How to use checksums
4fcc1820f77be9ef17e8ee377e5ea51df0f2ed75abcbd80ddf260c9374c16a0a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.6

Release history Release notifications | RSS feed

This release

1.1.0 This release

2 release files

1.0.0

2 release files

0.6.0

2 release files

0.5.3

2 release files

0.5.2

2 release files

0.5.1

2 release files

0.5.0

2 release files

0.4.1

2 release files

0.4.0

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.0

1 release file

0.1.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page