Skip to main content

A middleware for Django applications using Cloudflare as a proxy. Allows you to extract and access CF headers.

Project description

Build Status Python Versions Latest Version Docs

django-cloudflare

A reusable Django middleware that allows you to easily extract Cloudflare headers from incoming requests.

Resources:

Supported headers

Cloudflare header Default attribute Setting to enable
Cdn-Loop request.cf_cdn_loop CF_HEADER_CDN_LOOP_ENABLED
Cf-Connecting-Ip request.cf_ip CF_HEADER_IP_ENABLED
Cf-Connecting-Ipv6 request.cf_ipv6 CF_HEADER_IPV6_ENABLED
Cf-Ipcountry request.cf_country CF_HEADER_COUNTRY_ENABLED
Cf-Ray request.cf_ray CF_HEADER_RAY_ENABLED
Cf-Visitor request.cf_visitor CF_HEADER_VISITOR_ENABLED
Cf-Warp-Tag-Id request.cf_warp_tag CF_HEADER_WARP_TAG_ENABLED
X-Forwarded-For request.cf_forwarded_for CF_HEADER_FORWARDED_FOR_ENABLED
X-Forwarded-Proto request.cf_forwarded_proto CF_HEADER_FORWARDED_PROTO_ENABLED

All headers are disabled by default. Each attribute name can be customized via the corresponding *_ATTR_NAME setting.

Installation

pip install -U django-cloudflare

Usage

Add the middleware and enable the headers you need in your settings.py:

MIDDLEWARE += ["django_cloudflare.CloudflareMiddleware"]

CF_HEADER_IP_ENABLED = True
CF_HEADER_COUNTRY_ENABLED = True

Then access the values on the request object:

def my_view(request):
    ip = request.cf_ip
    country = request.cf_country

Both sync and async views are supported.

Requirements

Python 3.10+, Django 4.2+

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_cloudflare-1.0.0.tar.gz (64.2 kB view details)

Uploaded Source

Built Distribution

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

django_cloudflare-1.0.0-py3-none-any.whl (4.7 kB view details)

Uploaded Python 3

File details

Details for the file django_cloudflare-1.0.0.tar.gz.

File metadata

  • Download URL: django_cloudflare-1.0.0.tar.gz
  • Upload date:
  • Size: 64.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for django_cloudflare-1.0.0.tar.gz
Algorithm Hash digest
SHA256 f6d60184cf7c6486dd15d9cf87f0cf8ec790428e2a2ba0f84fcd7d28baf5132b
MD5 966eab26df776b45023898b226c935fa
BLAKE2b-256 c1150ce3e90ca7992287f191436c335c1d11c915254ce1101c935857e09d943e

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_cloudflare-1.0.0.tar.gz:

Publisher: release.yml on tomwojcik/django-cloudflare

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file django_cloudflare-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for django_cloudflare-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 cd522747217e65ec9e45966d846fdf63703edc0a2ec2e9165f8d664bd7a356f6
MD5 adbde77450631262e89d83784c7f8796
BLAKE2b-256 fd77858298c2592037257cbb13e346237209e82bdf3daab2c7e014a64d08420c

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_cloudflare-1.0.0-py3-none-any.whl:

Publisher: release.yml on tomwojcik/django-cloudflare

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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