Skip to main content

a framework for implementing rate-limiting middleware for Django projects

CI Status PyPi

Overview

This package allows Django developers to define application-level rate-limiting rules. Often, these rules would be expressed as “max # requests within a defined time period”. E.g.:

  • an IP address may make at most 1500 requests/day

  • users with an OAuth access token may make 500 reads/hour and 200 writes/hour

You can also define leaky bucket-style rules:

  • Allow 10 requests per minute, then every 6 seconds thereafter.

Features

  • Attach rules to specific views using a decorator

  • Supports multiple throttle configurations

  • Use Django’s cache layer as the storage backend, or use Redis scripting for production-ready atomic operations

  • Define request attributes to rate limit (e.g. remote IP address, username, HTTP header value, device fingerprint, etc.)

  • Application-level rate limiting rules using fixed-bucket or generic cell rate algorithm (leaky bucket)

Installation

  1. Install the library with pip:

    sudo pip install django-throttle-requests
  2. Add the directory throttle to your project’s PYTHONPATH.

Usage

  1. Insert the following configuration into your project’s settings:

    THROTTLE_ZONES = {
        'default': {
            'VARY':'throttle.zones.RemoteIP',
            'ALGORITHM': 'fixed-bucket',  # Default if not defined.
            'BUCKET_INTERVAL':15 * 60,  # Number of seconds to enforce limit.
            'BUCKET_CAPACITY':50,  # Maximum number of requests allowed within BUCKET_INTERVAL
        },
    }
    
    # Where to store request counts.
    THROTTLE_BACKEND = 'throttle.backends.cache.CacheBackend'
    
    # Optional if Redis backend is chosen ('throttle.backends.redispy.RedisBackend')
    THROTTLE_REDIS_HOST = 'localhost'
    THROTTLE_REDIS_PORT = 6379
    THROTTLE_REDIS_DB = 0
    THROTTLE_REDIS_AUTH = 'pass'
    
    # Normally, throttling is disabled when DEBUG=True. Use this to force it to enabled.
    THROTTLE_ENABLED = True
  2. Use the @throttle decorator to enforce throttling rules on a view:

    from throttle.decorators import throttle
    
    @throttle(zone='default')
    def myview(request):
       ...
  3. Also works with class-based views:

    from django.views.generic import View
    from django.utils.decorators import method_decorator
    
    from throttle.decorators import throttle
    
    class TestView(View):
    
        @method_decorator(throttle(zone='default'))
        def dispatch(self, *args, **kwargs):
            return super(TestView, self).dispatch(*args, **kwargs)
    
        def head(self, request):
            ...
    
        def get(self, request):
            ...
Code:

https://github.com/sobotklp/django-throttle-requests

Documentation:

https://readthedocs.org/projects/django-throttle-requests/

Development

Run unit tests:

uv run runtests.py

Run linter:

uvx ruff@latest check

Release files for django-throttle-requests 0.8.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-throttle-requests 0.8.0
File Size Uploaded
django_throttle_requests-0.8.0.tar.gz 9.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-throttle-requests 0.8.0
File Interpreter ABI Platform
django_throttle_requests-0.8.0-py3-none-any.whl Python 3 none any Details

Total release size: 21.1 kB

Release files / django_throttle_requests-0.8.0.tar.gz

Download URL django_throttle_requests-0.8.0.tar.gz
Size 9.1 kB
Tags Source
SHA-256 checksum
How to use checksums
ad01591765cf0901ee9be08fbfa70fd9c6cb7aa31e667c446a5d8c84ecb8cb09
BLAKE2b-256 checksum
How to use checksums
62bad9e955cdf695a6874c63463fd91dc4133fcab756719a4ce02b6438dcecd6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.9.18 {"installer":{"name":"uv","version":"0.9.18","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release files / django_throttle_requests-0.8.0-py3-none-any.whl

Download URL django_throttle_requests-0.8.0-py3-none-any.whl
Size 12.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
dd65c0a2a6625e1aad1fbc37038828a5b83211a66c0d1fdb139ae8670755c909
BLAKE2b-256 checksum
How to use checksums
7eec0378501d287be20b346ab618d5bbf7ca0a74ea44e6b8892ef2ccf914add0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.9.18 {"installer":{"name":"uv","version":"0.9.18","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release history Release notifications | RSS feed

This release

0.8.0 This release

2 release files

0.7.0

1 release file

0.6.0

1 release file

0.5.9

1 release file

0.5.8

1 release file

0.5.7

2 release files

0.5.6

1 release file

0.5.5

1 release file

0.5.4

1 release file

0.5.3

1 release file

0.5.2

2 release files

0.5.1

1 release file

0.5.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