'Is there a way to know if a website is in development mode or production mode in Django?
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
Solution 1:[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
Solution 2:[2]
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Mugoma |
| Solution 2 | TÃ i Hatranduc |
