Reputation: 95
I got this error when trying to host my django project on heroku FileNotFoundError: [Errno 2] No such file or directory: '/app/static'
here is settings.py:
STATIC_DIR = os.path.join(BASE_DIR, 'static')
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static'),
]
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
and this is the error :
File "/app/manage.py", line 22, in <module>
main()
File "/app/manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "/app/.heroku/python/lib/python3.9/site-packages/django/core/management/__init__.py", line 419, in execute_from_command_line
utility.execute()
File "/app/.heroku/python/lib/python3.9/site-packages/django/core/management/__init__.py", line 413, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/app/.heroku/python/lib/python3.9/site-packages/django/core/management/base.py", line 354, in run_from_argv
self.execute(*args, **cmd_options)
File "/app/.heroku/python/lib/python3.9/site-packages/django/core/management/base.py", line 398, in execute
output = self.handle(*args, **options)
File "/app/.heroku/python/lib/python3.9/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 187, in handle
collected = self.collect()
File "/app/.heroku/python/lib/python3.9/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 105, in collect
for path, storage in finder.list(self.ignore_patterns):
File "/app/.heroku/python/lib/python3.9/site-packages/django/contrib/staticfiles/finders.py", line 130, in list
for path in utils.get_files(storage, ignore_patterns):
File "/app/.heroku/python/lib/python3.9/site-packages/django/contrib/staticfiles/utils.py", line 23, in get_files
directories, files = storage.listdir(location)
File "/app/.heroku/python/lib/python3.9/site-packages/django/core/files/storage.py", line 316, in listdir
for entry in os.scandir(path):
FileNotFoundError: [Errno 2] No such file or directory: '/app/static'
Upvotes: 2
Views: 3267
Reputation: 3550
The differences between STATICFILES_DIRS
, STATIC_ROOT
, MEDIA_ROOT
, etc. can be confusing. In your case, you can start by removing the first line of your settings.py
, because STATIC_DIR
is not a Django settings variable.
Then, the actual cause of your problem is that directory specified in the Django's settings variable STATICFILES_DIRS
does not exist:
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static'),
]
On the Heroku server, this path is /app/static
, but on you own computer it likely lives in a location such as $HOME/awesome-django-project/static
. Creating the directory there will hopefully solve the problem.
Alternatively, you could remove the setting STATICFILES_DIRS
altogether and place your staticfiles inside app directories. I personally prefer this approach because it makes the settings simpler and your source code directory cleaner.
Upvotes: 2