I have a Django API that is being deployed in PythonAnywhere.
For that, I can not let the default option in settings.py
as True
:
DEBUG=True
However, the app also has a Swagger page using drf-yasg library. For that to work locally, I have to set the debug option as true.
However is being troublesome to maintain it, and I often mistakenly commit it as True to the repository.
Is there anyway I could manage this option so whenever I am running locally, it will automatically set it to True
, but leave it as False
by default?
DEBUG=False
CodePudding user response:
For this purpose I use python-decouple
package. To distinguish between production and development environments you can create one .env
file with all production-related variables in it. In settings.py
when retrieving the configuration parameters leave every default
option with the one used in development. So when .env
file is not present the default values are gonna be set.
from decouple import config
DEBUG = config('YOUR_ENV_VAR_DEBUG', default=True, cast=bool)
For more real-world use cases, please refer to the documentation.