I am working on a Google Cloud Django Project.
I am trying to check in settings.py
if I am running in Development or Production mode.
I added the following block of code to test if the SOFTWARE where the program is running either is on my machine or on Google Cloud Servers.
# | settings.py |
if os.getenv('SERVER_SOFTWARE', '').startswith('Google App Engine'):
#code to execute in production
else:
#code to execute in development
I noticed that the if
statment is always false, so I decited to debug the os.environ
dict.
The result of the debug is that the value of the key SERVER_SOFTWARE of the enviroment is equal to gunicorn/20.1.0.
As written in the correct answer of this stackoverflow question, when running on production (so on Google Cloud Severs App Engine), the value of SERVER_SOFTWARE should be Google App Engine/X.Y.Z, where X, Y and Z represent the version of Google Cloud. But, as I said, my value, when running on App Engine, is not like that, it is gunicorn/20.1.0.
So, how do I make the program know if I am running either in development or production?
I'll present a couple other solutions.
(1) GAE provided env vars
GAE automatically sets environment variables that you probably don't set when running locally. I use this:
version = os.environ.get('GAE_VERSION', 'local')
You'll get the actual version in production and 'local' on your machine.
(2) Check the request URL
This is Flask but there must be something similar in Django:
request.url_root == 'http://localhost:8080/'