Search code examples
djangodjango-authenticationstaging

How can I require admin login on a Django site only if a certain environment variable is set?


I have two AWS Elastic Beanstalk environments serving content using the same Django web server. One of them is a staging site (i.e. for development/testing purposes) and one of them is the production website. On the staging site, features that normally require payment are free to use in order to simplify testing and development. I want to avoid having users stumble upon this staging site and gaining free access to features that normally require payment.

To prevent this, I would like to require staff login for all of the views, but only on the staging site. I have an environment variable that gets checked in settings.py which determines whether or not the server is running in the staging or production environment. Is there some elegant/clean way of accomplishing this by checking that environment variable?


Solution

  • I would add a Middleware that checks for this condition to the MIDDLEWARE settings variable if the environment variable says you're on staging.