Custom Middleware in Django

## Create project/middleware/custom_middleware.py class IPAddressFilterMiddleware(object): def process_request(self, request): allowed_ips = ['192.168.1.1', '123.123.123.123', etc...] # Authorized ip's ip = request.META.get('REMOTE_ADDR') # Get client IP if ip not in allowed_ips: raise Http403 return None ## Add the "IPAddressFilterMiddleware" to "MIDDLEWARE" in Django settings
Custom Middleware in django is pretty easy .
do the follows :

create the directory inside your project called middlewares( your choice) , create the python file and put the code the inside the file and add the path in to your django project settings ....
you can change the "middleware behavior as per your needs"

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.