Skip to main content

A Django middleware to restrict incoming IPs to a Django project.

Project description

Django IP Restriction

Department of International Trade Django IP Whitelist. A Django middleware to restrict incoming IPs to a Django project to a list of allowed IPs or IP ranges. Access to the admin login screen, and access to authenticated users are configurable, so it can be set such that users can authenticate, and bypass the IP restriction for the site.


  • Python >= 3.3

  • Django >= 1.9

Quick start

  1. Install the package:

    $ pip install django-ip-restriction
  2. Add the middleware to your settings (Note: the minimum necessary other middleware components are Django’s SessionMiddleware and AuthenticationMiddleware, these are added by default to your settings):

    # Django 1.9
    # Django 1.10+


Turning on or off, and configuring the IP whitelist is done either via variables in your Django settings, or via environment variables. Values in Django settings take preference over values in the environment.

Turning on/off the middleware is done via RESTRICT_IPS, and the default value is False. Either set this variable to True in Django settings, or set a truthy value (e.g. ‘true’, ‘1’) in your environment.

Individual IPs can be whitelisted via ALLOWED_IPS, which is either a list of IP strings in Django settings, or a comma-separated list of IPs in the environment, e.g the following 2 are equivalent:

# in bash (spaces are disregarded, trailing commas are OK)
export ALLOWED_IPS=',,,'

# in (will override the above environment variable)
ALLOWED_IPS = ['', '', '']

IP ranges can be whitelisted via ALLOWED_IP_RANGES, which is either a list of IP range strings (CIDR notation) in Django settings, or a comma-separated list of IP ranges in the environment, e.g.:

# in bash

# in
ALLOWED_IPS = ['', '']

Regardless of the IP addresses/rages that are in the whitelist, access for all authenticated users can be allowed with ALLOW_AUTHENTICATED. If true, this will allow any valid sessions past the IP restriction.

Regardless of the IP addresses/rages that are in the whitelist, access to the admin URLs is also allowed past the IP restriction if ALLOW_ADMIN is true.

Setting both ALLOW_ADMIN and ALLOW_AUTHENTICATED to true is recommended, and will allow any user that can log in, to first access only the admin interface in order to authenitcate, and from then have access to all URLs for the project.

Restict Admin views only

To restrict admin views only set the variables RESTRICT_ADMIN_BY_IPS, ALLOWED_ADMIN_IPS and ALLOWED_ADMIN_IP_RANGES


Contributions are welcome. Please follow the guidelines below to make life easier:

  • Fork the repo, branch off release, make changes, then make a pull request (PR) to release on the main repo

  • Include documentation for any new features

  • Please limit changes for a PR to a single feature, or a single bugfix
    • Make multiple PRs for multiple discrete changes

  • Please squash commits - ideally a single commit, but at least to a sensible minimum
    • If a PR reasonably should have multiple commits, consider if it should actually be separate PRs


MIT licensed. See the bundled LICENSE file for more details.


  • Allow the IP restriction to work in a blacklisting mode, rather than just a whitelisting mode

  • Get continuous integration to run on multiple python versions from 3.0+
    • Currently only running on 3.5.0

    • Utilise parallelism

  • Run tests on multiple Django versions
    • Currently only running against Django 1.9

    • Utilise parallelism

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-ip-restriction-1.1.1.tar.gz (5.4 kB view hashes)

Uploaded source

Built Distribution

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Huawei Huawei PSF Sponsor Microsoft Microsoft PSF Sponsor NVIDIA NVIDIA PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page