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
- Add
ip_restrictor.middleware.IPRestrictMiddlewareto yourMIDDLEWAREsetting insettings.py:
MIDDLEWARE = [
# ... other middleware ...
'ip_restrictor.middleware.IPRestrictMiddleware',
# ... other middleware ...
]
- 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
- Set the default restriction mode (optional):
DEFAULT_IP_RESTRICTION_MODE = "WHITELIST" # or "BLACKLIST" or False (default)
"WHITELIST": Only allows access from IPs inWHITELIST_IPS."BLACKLIST": Blocks access from IPs inBLACKLIST_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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
702aa3e7a6d9622b76052440568939ad7dfcb5f0ee7abc5c9ab892b0e793cbd1
|
|
| MD5 |
5e10d2f28c0198d978174fcf4fa071fe
|
|
| BLAKE2b-256 |
dd516695509b25188de879cd6777c7d1218a5963a928359ce7f7c67a57854e0a
|