Is there a way to know if a website is in development mode or production mode in Django?

1.2k Views Asked by At

In Django, Because when in development and production mode, the settings.py file has to be so much different
For example

Development:

DEBUG = true
...
ALLOWED_HOSTS = []
...
EMAIL_PAGE_DOMAIN = 'http://127.0.0.1:8000'

Production:

DEBUG = false
...
ALLOWED_HOSTS = ['example.com']
...
EMAIL_PAGE_DOMAIN = 'https://example.com'

I don't know if there is a condition to check if the app is in development mode or production mode so that I don't hard-code it. The code should change automatically based on its mode. I imagine something like this

if in_Development_Mode() == true:
   #code for devopmenet
else:
   #code for production
3

There are 3 best solutions below

0
On

You can use os.getcwd()

If the current working directory is not the local directory then the website is in production

if 'C:\\Users\\UserName' in os.getcwd():
    DEBUG = True else:
    DEBUG = False
0
On

Another way to validate this using the os.getcwd() is to check for the production application folder.

if os.getcwd() == '/app':
    DEBUG = False # Set to false when in production
else:
    DEBUG = True # Set to true when not in production

Can use this for future reference as it can come in handy.

2
On

Yes there is:

from django.conf import settings

if settings.DEBUG==True:
   #code for development
else:
   #code for production

https://docs.djangoproject.com/en/3.1/topics/settings/#using-settings-in-python-code