Skip to content
Advertisement

Determine if Django is running under the development server

Is there a way to determine if Django is running on localhost and setting the DEBUG variable in settings.py accordingly.

So that if I run the server locally it will set DEBUG to True and otherwise set it to False.

Localhost: python manage.py runserver
Not localhost: python manage.py runserver 0.0.0.0:8000

Advertisement

Answer

As suggested by Bernhard Vallant, you can just check for runserver in sys.argv.

You can just replace your DEBUG assignment in settings.py with this:

DEBUG = (sys.argv[1] == 'runserver')

You should also import sys somewhere in settings.py.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement