Metadata-Version: 2.4
Name: django-cloudflare
Version: 1.0.0
Summary: A middleware for Django applications using Cloudflare as a proxy. Allows you to extract and access CF headers.
Project-URL: Homepage, https://github.com/tomwojcik/django-cloudflare
Project-URL: Repository, https://github.com/tomwojcik/django-cloudflare
Project-URL: Documentation, https://django-cloudflare.readthedocs.io/
Project-URL: Issues, https://github.com/tomwojcik/django-cloudflare/issues
Author-email: Tom Wojcik <django-cloudflare-pkg@tomwojcik.com>
License: MIT
License-File: LICENSE
Keywords: cloudflare,django
Classifier: Development Status :: 5 - Production/Stable
Classifier: Framework :: Django
Classifier: Framework :: Django :: 4.2
Classifier: Framework :: Django :: 5.1
Classifier: Framework :: Django :: 5.2
Classifier: Intended Audience :: Developers
Classifier: Intended Audience :: Information Technology
Classifier: Intended Audience :: System Administrators
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Internet
Requires-Python: >=3.10
Requires-Dist: django>=4.2
Description-Content-Type: text/markdown

[![Build Status](https://github.com/tomwojcik/django-cloudflare/workflows/CI/badge.svg)](https://github.com/tomwojcik/django-cloudflare/actions/workflows/ci.yml)
[![Python Versions](https://img.shields.io/pypi/pyversions/django-cloudflare.svg)](https://pypi.org/project/django-cloudflare/)
[![Latest Version](https://img.shields.io/pypi/v/django-cloudflare.svg)](https://pypi.org/project/django-cloudflare/)
[![Docs](https://readthedocs.org/projects/django-cloudflare/badge/?version=latest)](https://django-cloudflare.readthedocs.io/en/latest/)

# django-cloudflare

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

Resources:

* **Source**: https://github.com/tomwojcik/django-cloudflare
* **Documentation**: https://django-cloudflare.readthedocs.io/
* **Changelog**: https://django-cloudflare.readthedocs.io/en/latest/changelog/

## 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

```bash
pip install -U django-cloudflare
```

## Usage

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

```python
MIDDLEWARE += ["django_cloudflare.CloudflareMiddleware"]

CF_HEADER_IP_ENABLED = True
CF_HEADER_COUNTRY_ENABLED = True
```

Then access the values on the request object:

```python
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+
