django-allow-cidr is a Django Middleware that extends the functionality of Django's `ALLOWED_HOSTS` setting to support CIDR IP ranges. It allows developers to specify IP networks (e.g., '192.168.1.0/24') alongside regular hostnames, which is particularly useful for health checks or internal network access. The current version is 0.8.0, with releases occurring periodically to support new Django and Python versions.
pip install django-allow-cidrVerified import paths — ran on the pinned version, not inferred.
To use django-allow-cidr, install it via pip and then add `AllowCIDRMiddleware` to the very beginning of your `MIDDLEWARE` settings. Define your allowed CIDR ranges in the `ALLOWED_CIDR_NETS` setting in your `settings.py`. Note that if `ALLOWED_CIDR_NETS` is defined, the middleware will internally set `ALLOWED_HOSTS` to `['*']` and take over host header validation.
Upgrade to Python 3.9+ and Django 4.1+ before upgrading django-allow-cidr to 0.8.0.
Ensure your project runs on Python 3.7 or newer before upgrading to django-allow-cidr 0.6.0+.
Always place `'allow_cidr.middleware.AllowCIDRMiddleware'` at the very top of your `MIDDLEWARE` setting.
Configure `ALLOWED_HOSTS` and `ALLOWED_CIDR_NETS` as usual. Be aware that `ALLOWED_HOSTS = ['*']` becomes the effective Django setting at runtime when `ALLOWED_CIDR_NETS` is in use, with `django-allow-cidr` enforcing the actual host restrictions.
Ensure your environment is running Python 3.3+ (where `ipaddress` was introduced). No action is typically needed unless you had specific `netaddr` version requirements that are no longer met.
Ensure the package is installed using `pip install django-allow-cidr` and that the middleware is correctly added to your `settings.py` as `'allow_cidr.middleware.AllowCIDRMiddleware'`.
Verify that `'allow_cidr.middleware.AllowCIDRMiddleware'` is the *first* middleware listed in your `MIDDLEWARE` setting and that `ALLOWED_CIDR_NETS` in `settings.py` contains the appropriate CIDR range for the requesting IP (e.g., `ALLOWED_CIDR_NETS = ['192.168.1.0/24']`).
Review your `ALLOWED_CIDR_NETS` list in `settings.py` and ensure all strings are in a correct CIDR format (e.g., `'192.168.1.0/24'` or `'2001:db8::/32'`).