Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django collectstatic no such file or directory

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?

like image 527
Victor Polevoy Avatar asked Sep 23 '14 08:09

Victor Polevoy


People also ask

How do I serve static files in Django?

Configuring static files Make sure that django.contrib.staticfiles is included in your INSTALLED_APPS . In your templates, use the static template tag to build the URL for the given relative path using the configured STATICFILES_STORAGE . Store your static files in a folder called static in your app.

What are static files in Django?

Aside from the HTML generated by the server, web applications generally need to serve additional files — such as images, JavaScript, or CSS — necessary to render the complete web page. In Django, we refer to these files as “static files”.

What is static root?

STATIC_ROOT is the single root directory from where the Django application will serve the static files in production. While deployment you would typically want to serve the static files from the conventional /var/www/example.com.


1 Answers

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.

like image 118
doniyor Avatar answered Sep 21 '22 07:09

doniyor