When I deployed django app by heroku, I got error.
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(file)))
Setting.py has this BASE_DIR.
How do I resolve this error?
Traceback (most recent call last):
File "manage.py", line 22, in <module>
main()
File "manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "/app/.heroku/python/lib/python3.8/site-packages/django/core/management/__init__.py", line 446, in execute_from_command_line
utility.execute()
File "/app/.heroku/python/lib/python3.8/site-packages/django/core/management/__init__.py", line 386, in execute
settings.INSTALLED_APPS
File "/app/.heroku/python/lib/python3.8/site-packages/django/conf/__init__.py", line 87, in __getattr__
self._setup(name)
File "/app/.heroku/python/lib/python3.8/site-packages/django/conf/__init__.py", line 74, in _setup
self._wrapped = Settings(settings_module)
File "/app/.heroku/python/lib/python3.8/site-packages/django/conf/__init__.py", line 183, in __init__
mod = importlib.import_module(self.SETTINGS_MODULE)
File "/app/.heroku/python/lib/python3.8/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
File "<frozen importlib._bootstrap>", line 991, in _find_and_load
File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 848, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/app/MovieReview/settings.py", line 77, in <module>
'DIRS': [BASE_DIR / 'templates'],
TypeError: unsupported operand type(s) for /: 'str' and 'str'
CodePudding user response:
It's because you cannot use the /
operator between strings.
You should either use this:
"DIRS": [os.path.join(BASE_DIR, "templates")]
or use the pathlib
to create BASE_DIR
path:
from pathlib import Path
BASE_DIR = Path(__file__).resolve().parent.parent
The pathlib
enables you to use /
operator to join multiple paths:
from pathlib import Path
p1 = Path('/tmp')
p2 = p1 / 'dir1' # it's similiar to os.path.join(p1, 'dir1')