---
title: "Django Cloudflare R2 Integration"
url: "https://ridwaanhall.com/projects/django-cloudflare-r2-integration"
markdown: "https://ridwaanhall.com/projects/django-cloudflare-r2-integration.md"
summary: "A Django web application that integrates Cloudflare R2 as a cost-effective, S3-compatible storage backend for static and media files."
---

# Django Cloudflare R2 Integration

> A Django web application that integrates Cloudflare R2 as a cost-effective, S3-compatible storage backend for static and media files.

- Category: web-development
- Status: Completed
- Started: September 17, 2025
- Built with: Django, Python, Cloudflare, boto3

## About

### The problem

Serving static and media files from S3-style storage is standard for Django in production, but egress fees grow with traffic, and setting up an alternative correctly is poorly documented.

### The approach

I built a reference Django project that uses Cloudflare R2 through its S3-compatible API, with separate storage backends for static files and user uploads on top of django-storages and boto3.

- Dedicated storage classes for static assets and media, configured through environment variables with python-decouple.
- Examples of FileField and ImageField uploads served from R2 through Cloudflare's network.
- A production checklist covering buckets, API token permissions, CORS, bucket policy and collectstatic.

### The result

The project is a ready-to-copy reference that lets Django developers cut storage costs and serve files from Cloudflare's edge without leaving Django's familiar storage API.

## Features

- **Cloudflare R2 Storage Integration**: Configures Django to use Cloudflare R2 as the storage backend for static and media files.
- **Environment-Based Configuration**: Manages sensitive credentials and settings via environment variables using python-decouple.
- **Custom Storage Classes**: Provides dedicated classes for StaticFilesStorage and MediaFileStorage in Django.
- **S3-Compatible Interface**: Works with R2 through boto3, using the AWS S3-compatible API.
- **Production-Ready Setup**: Includes deployment checklist, security best practices, and collectstatic integration.

## Tags

Django, cloudflare, r2, s3-compatible, storage, boto3, Python, django-storages, Deployment, static-media

## Elsewhere

- This page for people: https://ridwaanhall.com/projects/django-cloudflare-r2-integration
- Index of every page as Markdown: [llms.txt](https://ridwaanhall.com/llms.txt)
