Metadata-Version: 2.4
Name: django-brotli
Version: 0.5.0
Summary: Middleware that compresses response using brotli algorithm.
Keywords: django,brotli,compression,middleware
Author: Václav Dohnal
Author-email: Václav Dohnal <vaclav.dohnal@gmail.com>
License-Expression: MIT
License-File: LICENSE
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: Natural Language :: English
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: Programming Language :: Python :: 3.15
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Operating System :: OS Independent
Classifier: Environment :: Web Environment
Classifier: Framework :: Django
Classifier: Framework :: Django :: 5.2
Classifier: Framework :: Django :: 6.0
Classifier: Framework :: Django :: 6.1
Requires-Dist: django>=5.2
Requires-Dist: brotli>=1.1
Requires-Python: >=3.11
Project-URL: homepage, https://github.com/illagrenan/django-brotli
Project-URL: repository, https://github.com/illagrenan/django-brotli
Project-URL: bug-tracker, https://github.com/illagrenan/django-brotli/issues
Project-URL: changelog, https://github.com/illagrenan/django-brotli/blob/master/CHANGELOG.md
Description-Content-Type: text/markdown

# Django Brotli: *Middleware that compresses response using brotli algorithm*

[![License: MIT](https://img.shields.io/badge/license-MIT-green.svg)](https://opensource.org/licenses/MIT)
[![pypi](https://img.shields.io/badge/code%20style-The%20Ruff%20Formatter-000000.svg)](https://docs.astral.sh/ruff/formatter/)
[![pypi](https://img.shields.io/pypi/v/django-brotli.svg)](https://pypi.org/project/django-brotli/)
[![Python version](https://img.shields.io/pypi/pyversions/django-brotli.svg?logo=python&logoColor=white&label=python)](https://pypi.org/project/django-brotli/)
![Supported Django](https://img.shields.io/badge/django%20versions-%5E5.2%20||%20%5E6.0-blue.svg?logo=django&logoColor=white)
<br>
[![Build Status](https://github.com/illagrenan/django-brotli/actions/workflows/development.yml/badge.svg)](https://github.com/illagrenan/django-brotli/actions/workflows/development.yml)
[![codecov](https://codecov.io/gh/illagrenan/django-brotli/branch/master/graph/badge.svg)](https://codecov.io/github/illagrenan/django-brotli)

* PyPI: <https://pypi.org/project/django-brotli/>
* License: [MIT](https://choosealicense.com/licenses/mit/)

## Introduction

This project consists of `BrotliMiddleware` which works the same as Django `GZipMiddleware` ([Docs](https://docs.djangoproject.com/en/dev/ref/middleware/#module-django.middleware.gzip)/[Source](https://github.com/django/django/blob/master/django/middleware/gzip.py#L10-L52)). `BrotliMiddleware` will compress content of HTTP response using brotli algorithm (Brotli Compressed Data Format is defined in [RFC 7932](https://www.ietf.org/rfc/rfc7932.txt)).

In November 2016 is brotli supported by Firefox, Chrome, Android Browser and Opera (detailed stats on [caniuse](http://caniuse.com/#search=brotli)). Brotli is applied only when client has sent `Accept-Encoding` header containing `br`.

## Installation

1. **Requirements**: Python `>=3.11` and Django `>=5.2`. Install the package:
    ```console linenums="0"
    uv add django-brotli
    ```

    _or_

    ```console linenums="0"
    poetry add django-brotli@latest
    ```

    _or_

    ```console linenums="0"
    pip install --upgrade django-brotli
    ```

2. Add `django_brotli.middleware.BrotliMiddleware` to `MIDDLEWARE`:
    ```python
    MIDDLEWARE = [
        "django_brotli.middleware.BrotliMiddleware",
        # ...
    ]
    ```

## Configuration

Set these environment variables before Django starts; they are read once on import.

| Variable         | Default        | Values                                    |
|------------------|----------------|-------------------------------------------|
| `BROTLI_QUALITY` | `4`            | `0` (fastest) to `11` (smallest output)   |
| `BROTLI_MODE`    | `MODE_GENERIC` | `MODE_GENERIC`, `MODE_TEXT`, `MODE_FONT`  |

The default quality `4` is about 2× faster than Django's `GZipMiddleware` and gives about 20% smaller output ([#117](https://github.com/illagrenan/django-brotli/pull/117)).

## Credits and Resources

- [Brotli on Wikipedia](https://en.wikipedia.org/wiki/Brotli)
- [Brotli compression format repository by Google](https://github.com/google/brotli)

## Contributing

1. Clone this repository (`git clone ...`)
2. Install package dependencies: `uv sync`
3. Change some code
4. Run linters and tests: `uv run task lint coverage`
5. Submit PR :)

## License

[The MIT License (MIT)](./LICENSE)
