Working on Django projects I find very often that many developers access the values that are defined in settings in this way
|
|
What happens if MY_SETTING
has not been defined in settings.py?
The code will raise an error and crash, of course. How can we make the
code more reliable? We could try/except the code block that tries to
read the value and maybe set a value if we get an exception, but this
would not be a clean way to do this job.
A cleaner way to do it is to use getattr in this way:
|
|
getattr will try to read MY_SETTING
value from settings.py,
if the value doesn’t exist my_value
will be assigned with
'my-default-value'