0

So I have a Django app that serves as the backend for another app that I've written. I only want my Django app to be accessed from this other app, which will probably have a handful of versions (production, staging, dev1, dev2). How can I configure my Django app to only accept connections from these handful of clients?

Burhan Khalid
  • 169,990
  • 18
  • 245
  • 284
tadasajon
  • 14,276
  • 29
  • 92
  • 144

1 Answers1

2

If I understand you well you want to control the access to your Django App, one option is to add a custom Middleware to check the IP of the user and if it's not allowed you can show an error, or redirect to other site or whatever you want.

class CheckIPMiddleware(object):
    # Check if client IP is allowed
    def process_request(self, request):
        allowed_ips = ['192.168.1.1', '123.123.123.123', etc...]
        ip = request.META.get('REMOTE_ADDR')  # Get client IP
        if ip not in allowed_ips:
           # Here you can raise a 403 Forbidden
           # or redirect to any other site/page

        # If user is allowed nothing happens
        return None

If this is useful to you you have to remember 2 things:

  • Add code to a file following this path: your_project/middleware/checkipmiddleware.py
  • Edit your settings and add your_project_name.middleware.checkipmiddleware.CheckIPMiddleware into your middleware ( look for MIDDLEWARE_CLASSES = (... in your settings.py )
AlvaroAV
  • 10,335
  • 12
  • 60
  • 91