Opened 10 years ago
Last modified 10 years ago
#23436 closed Cleanup/optimization
Should use abspath for default settings.BASE_DIR — at Initial Version
Reported by: | Harry Percival | Owned by: | nobody |
---|---|---|---|
Component: | Core (Other) | Version: | 1.6 |
Severity: | Normal | Keywords: | |
Cc: | Triage Stage: | Accepted | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
ref the default project template's settings.py, BASE_DIR is defined as:
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
Because there's no abspath, you can get different results for BASE_DIR depending on how settings.py is run. This is an edge case, since settings.py is almost always imported by django in the right context, but still -- compare:
python manage.py shell
from django.conf import settings
print(BASE_DIR)
With:
python -i myproject/settings.py
print(BASE_DIR)
As an aside, in general, in any path wrangling you should always call abspath first. Try running this script, saved to a file:
import os
print(os.path.abspath(os.path.dirname(os.path.dirname(file))))
print(os.path.dirname(os.path.dirname(os.path.abspath(file))))
As to tests, the only one I can think of would be a pretty horrible one involving actually callling out using subprocess.Popen to a python -i of the project template settings file, so maybe ok not to bother?
credit to @CleanCut for turning me onto this weirdness!