2

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
T H
  • 423
  • 4
  • 7

3 Answers3

1

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

Mugoma
  • 294
  • 2
  • 8
  • Thank you. In that way, I have to manually write "DEBUG = False" every time I upload the project to the server but I want that the condition automatically changes whether I write "DEBUG = False" or not – T H Dec 28 '20 at 16:21
  • @Tai If you have different settings.py files for production and development, the code may not need altering. – Mugoma Dec 29 '20 at 04:33
0

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
T H
  • 423
  • 4
  • 7
0

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.

Damoiskii
  • 1,328
  • 1
  • 5
  • 20