Unneeded DATA_DIR kludge
See original GitHub issueI got my project generated, and I’m looking at the diffs between it and a Django-CMS 3.5 project I set up a year ago, and there’s this weirdness at the start of settings.py
:
import os # isort:skip
gettext = lambda s: s
DATA_DIR = os.path.dirname(os.path.dirname(__file__))
"""
Django settings for abcde project.
Generated by 'django-admin startproject' using Django 2.1.9.
For more information on this file, see
https://docs.djangoproject.com/en/2.1/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/2.1/ref/settings/
"""
import os
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
There’s a few problems here:
1. You can’t put code in front of the file’s docstring. Python will ignore it; it is not placed in the module’s __doc__
property:
$ ./manage.py shell
>>> import abcde.settings as tgt_settings
>>> repr(tgt_settings.__doc__)
'None'
Worse, while Python itself ignores those strings, other tools such as epydoc consider strings after variable assignments to be docstrings for those variables (e.g. for documenting module constants).
2. DATA_DIR
has the same value as BASE_DIR
, and BASE_DIR
’s algorithm is more robust. DATA_DIR
should be set equal to BASE_DIR
.
3. Setting gettext
to a no-op without documenting why, when we should be trying to import the proper gettext function, and only setting the no-op version if the import fails or something.
4. The os
module is imported twice.
Corrected version:
"""
Django settings for abcde project.
Generated by 'django-admin startproject' using Django 2.1.9.
For more information on this file, see
https://docs.djangoproject.com/en/2.1/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/2.1/ref/settings/
"""
import os
try:
from gettext import gettext
except ImportError:
def gettext(s):
return s
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
DATA_DIR = os.environ.get('DATA_DIR', BASE_DIR)
Issue Analytics
- State:
- Created 4 years ago
- Comments:8 (4 by maintainers)
Top GitHub Comments
@bluegrounds
gettext
can be probably removed now as it should be safe to use the “real” gettext_lazy in settings. this used to be impossibile insettings.py
and it remained in the generated settings file. We are testing a more intelligent way to patchsettings.py
viaast
module but it will take some time for it to be mergeable (a few months). In the meantime we don’t plan any cleanup on settings fileDATA_DIR
is used to setMEDIA_ROOT
,STATIC_ROOT
we will revise this upon the refactoring of the settings generation