19

In django 1.7 collectstatic throws an exception for me:

OSError: [Errno 2] No such file or directory: '/static'

I've performed a lot of collectstatic-calls and everything worked fine, but today have this issue.

settings.py

BASE_DIR = os.path.dirname(os.path.realpath(__file__))
INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',

    'fxblog',
    'rest_framework',
)

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, STATIC_URL.strip("/"))

STATICFILES_DIRS = (
    '/static/',
    '/upload/',
)

BASE_DIR is correct, checked it. Directory BASE_DIR/static/ exists and all my static files are there.

Traceback:

Traceback (most recent call last):
  File "../manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 399, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 392, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 242, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 285, in execute
    output = self.handle(*args, **options)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 415, in handle
    return self.handle_noargs(**options)
  File "/usr/local/lib/python2.7/dist-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 173, in handle_noargs
    collected = self.collect()
  File "/usr/local/lib/python2.7/dist-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 103, in collect
    for path, storage in finder.list(self.ignore_patterns):
  File "/usr/local/lib/python2.7/dist-packages/django/contrib/staticfiles/finders.py", line 106, in list
    for path in utils.get_files(storage, ignore_patterns):
  File "/usr/local/lib/python2.7/dist-packages/django/contrib/staticfiles/utils.py", line 25, in get_files
    directories, files = storage.listdir(location)
  File "/usr/local/lib/python2.7/dist-packages/django/core/files/storage.py", line 249, in listdir
    for entry in os.listdir(path):
OSError: [Errno 2] No such file or directory: '/static'

Any suggestions?

VP.
  • 15,509
  • 17
  • 91
  • 161
  • `STATIC_ROOT = os.path.join(BASE_DIR, 'sitestatic')` – Gaurav Jain Sep 23 '14 at 09:08
  • Same output - `OSError: [Errno 2] No such file or directory: '/static'`but directory (in the output) changed to sitestatic. – VP. Sep 23 '14 at 09:11
  • what about this? ``STATIC_ROOT = os.path.join(BASE_DIR, '/static')``. this should work. ``strip`` strips both ``/`` but it needs ``/static`` – doniyor Sep 23 '14 at 09:15
  • @doniyor Now: `django.core.exceptions.ImproperlyConfigured: The STATICFILES_DIRS setting should not contain the STATIC_ROOT setting` – VP. Sep 23 '14 at 09:29
  • @VictorPolevoy `STATIC_ROOT = os.path.join(BASE_DIR, 'sitestatic')` run collectstatic after updating STATIC_ROOT variable – Gaurav Jain Sep 23 '14 at 09:30
  • @GauravJain I've already did it and posted output in the comment. It changed directory to `sitestatic` but exception body is the same. – VP. Sep 23 '14 at 09:31
  • @VictorPolevoy leave ``STATICFILES_DIRS`` empty. you dont need this here – doniyor Sep 23 '14 at 09:35

4 Answers4

27

try this:

STATIC_ROOT = os.path.join(BASE_DIR, 'static')

and leave this empty, since you are using STATIC_ROOT

STATICFILES_DIRS = (
  # Put strings here, like "/home/html/static" or "C:/www/django/static".
  # Always use forward slashes, even on Windows.
  # Don't forget to use absolute paths, not relative paths.
)

it should work this way.

doniyor
  • 36,596
  • 57
  • 175
  • 260
  • 1
    Unfortunately, I've tried @fragles's solution first, so I can only upvote your answer. – VP. Sep 23 '14 at 09:40
7

Files in STATICFILES_DIRS need to have absolute path. Use normpath.

STATICFILES_DIRS = (
    normpath(join(BASE_DIR, 'static')),
    normpath(join(BASE_DIR, 'upload')),
)

Also it is good to set STATIC_ROOT to something like

STATIC_ROOT = normpath(join(BASE_DIR, 'assets'))

and STATIC_URL

STATIC_URL = '/static/'
fragles
  • 680
  • 6
  • 19
1

So I was having similar problem and followed what fraggles had suggested, but I was getting this follow up error:

NameError: name 'normpath' is not defined

My work around was switching the '.dirname' keyword for '.normpath' keyword instead:

STATICFILES_DIRS = (
    os.path.join(os.path.normpath(BASE_DIR), "static")
)

And it worked like magic. Hopefully this solution also helps someone.

Vick Swift
  • 2,945
  • 1
  • 15
  • 17
0

Just do this !

STATIC_URL = '/static/'
STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'static'),
]
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
  • and then run python manage.py collectstatic
  • this will create a new file staticfiles and will move all your static files inside it.
  • after that load static in your django template by using {% load static %} inside your head tag
Ashish Singh
  • 97
  • 1
  • 3