Django Ratelimit is a cache-based rate-limiting library for Django applications, currently at version 4.1.0. It provides decorators and middleware to limit the rate of client requests, helping to prevent abuse and manage server resources. The library typically has an active release cadence, with major versions aligning with Django's own release cycle and minor versions for fixes and features.
pip install django-ratelimitVerified import paths — ran on the pinned version, not inferred.
To use `django-ratelimit`, first ensure you have a Django cache backend configured that supports atomic increment operations (like Memcached or Redis, not the default database cache). Then, apply the `@ratelimit` decorator to your Django views. The `key` parameter determines how requests are grouped (e.g., by 'ip' or 'user'), `rate` defines the limit (e.g., '5/m' for 5 per minute), and `block=True` will return a 429 Too Many Requests response if the limit is exceeded.
Update all `import` statements from `ratelimit` to `django_ratelimit`.
To retain the old behavior of only annotating the request, explicitly set `block=False` on the decorator: `@ratelimit(key='ip', rate='5/m', block=False)`.
Ensure your project uses Python 3.7+ and Django 3.2+ when upgrading to `django-ratelimit` 4.0.0 or newer.
Configure your Django `CACHES` setting to use a backend like `django.core.cache.backends.memcached.PyMemcacheCache` or `django_redis.cache.RedisCache`.
For class-based views, wrap `@ratelimit` with `@method_decorator`. Example: `from django.utils.decorators import method_decorator; @method_decorator(ratelimit(key='ip', rate='5/m'), name='dispatch') class MyView(View): ...`