I wonder if these configs are the same:
Config 1:
EMAIL_PORT=587
EMAIL_USE_TLS=True
EMAIL_USE_SSL=None
Config 2:
EMAIL_PORT='587'
EMAIL_USE_TLS='True'
EMAIL_USE_SSL='None'
settings.py (or just a random python file):
EMAIL_PORT = os.getenv('EMAIL_PORT')
EMAIL_USE_TLS = os.getenv('EMAIL_USE_TLS')
EMAIL_USE_SSL = os.getenv('EMAIL_USE_SSL')
Will my, let's say EMAIL_PORT in settings.py get a value of 587 or it will be '587' instead, or if EMAIL_USE_TLS will get the pythonic boolean True
or 'True'
instead? What is the suggestion on assigning variables in envfile? How to handler such booleans, integers or any other non-string values?
thanks!
CodePudding user response:
As mentioned in the Python Docs os.getenv
will always return a str
:
key, default and the result are str.
(emphasis mine)
For the port you can simply do this:
EMAIL_PORT = int(os.getenv('EMAIL_PORT'))
But for the Boolean I would set a value of 0 or 1 in the env so you can do the following:
Config:
EMAIL_USE_TLS=1
Python:
EMAIL_USE_TLS = bool(int(os.getenv('EMAIL_USE_TLS')))
Both these methods rely on the correctness of the environment variables so you might want to add error handling.