Skip to main content

A Django package to restrict views based on IP addresses.

Project description

django-ip-restrictor

A simple Django package to restrict access to views based on IP addresses.

Installation

pip install django-ip-restrictor

Configuration

  1. Add ip_restrictor.middleware.IPRestrictMiddleware to your MIDDLEWARE setting in settings.py:
MIDDLEWARE = [
    # ... other middleware ...
    'ip_restrictor.middleware.IPRestrictMiddleware',
    # ... other middleware ...
]
  1. Configure allowed and blocked IP addresses in your settings.py:
WHITELIST_IPS = ["127.0.0.1", "192.168.1.0/24"]  # Allow localhost and the 192.168.1.0/24 subnet
BLACKLIST_IPS = ["10.0.0.5", "10.0.0.6"]  # Block these specific IPs
  1. Set the default restriction mode (optional):
DEFAULT_IP_RESTRICTION_MODE = "WHITELIST"  # or "BLACKLIST" or False (default)
  • "WHITELIST": Only allows access from IPs in WHITELIST_IPS.
  • "BLACKLIST": Blocks access from IPs in BLACKLIST_IPS.
  • False: No default restriction; use decorators or mixins for specific views.

Usage

1. Decorators:

Use the @allow_whitelist and @deny_blacklist decorators to restrict access to function-based views:

from ip_restrictor.decorators import allow_whitelist, deny_blacklist

@allow_whitelist
def my_protected_view(request):
    # ... your view logic ...
    return HttpResponse("This view is only accessible from whitelisted IPs.")


@deny_blacklist
def another_protected_view(request):
    # ... your view logic ...
    return HttpResponse("This view blocks blacklisted IPs.")

2. Mixins:

Use the WhitelistIPMixin and BlackListIPMixin for class-based views:

from ip_restrictor.mixins import WhitelistIPMixin, BlackListIPMixin
from django.views.generic import TemplateView

class ProtectedCBV(WhitelistIPMixin, TemplateView):
    template_name = "my_template.html"


class AnotherProtectedCBV(BlackListIPMixin, TemplateView):
    template_name = "another_template.html"

3. Middleware (Global Restriction):

The middleware applies the restrictions based on DEFAULT_IP_RESTRICTION_MODE. If the mode is set to "WHITELIST", only whitelisted IPs will be allowed access to any view. If set to "BLACKLIST", blacklisted IPs will be denied access.

Handling X-Forwarded-For

The middleware and decorators handle the X-Forwarded-For header, so it should work correctly with reverse proxies.

Error Handling

If an IP address is not allowed, the middleware or decorator will return an HttpResponseForbidden (403). You can customize this behavior by implementing custom error handling middleware or overriding the decorator/mixin functionality.

Example settings.py

MIDDLEWARE = [
    # ... other middleware ...
    'ip_restrictor.middleware.IPRestrictMiddleware',
]

WHITELIST_IPS = ["127.0.0.1", "192.168.0.0/16"]
BLACKLIST_IPS = ["10.0.0.0/8"]
DEFAULT_IP_RESTRICTION_MODE = "WHITELIST" # Or "BLACKLIST" or False

Project details


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_restrictor-0.1.0.tar.gz (4.1 kB view details)

Uploaded Source

File details

Details for the file django_ip_restrictor-0.1.0.tar.gz.

File metadata

  • Download URL: django_ip_restrictor-0.1.0.tar.gz
  • Upload date:
  • Size: 4.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.3

File hashes

Hashes for django_ip_restrictor-0.1.0.tar.gz
Algorithm Hash digest
SHA256 702aa3e7a6d9622b76052440568939ad7dfcb5f0ee7abc5c9ab892b0e793cbd1
MD5 5e10d2f28c0198d978174fcf4fa071fe
BLAKE2b-256 dd516695509b25188de879cd6777c7d1218a5963a928359ce7f7c67a57854e0a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page