【问题标题】:Django collectstatic no such file or directoryDjango collectstatic 没有这样的文件或目录
【发布时间】:2014-11-17 09:58:09
【问题描述】:

在 django 1.7 中,collectstatic 为我抛出异常:

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

我已经执行了很多 collectstatic-calls 并且一切正常,但是今天遇到了这个问题。

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 正确,检查一下。目录 BASE_DIR/static/ 存在,我所有的静态文件都在那里。

追溯:

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'

有什么建议吗?

【问题讨论】:

  • STATIC_ROOT = os.path.join(BASE_DIR, 'sitestatic')
  • 相同的输出 - OSError: [Errno 2] No such file or directory: '/static'but 目录(在输出中)更改为 sitestatic。
  • 这个怎么样? STATIC_ROOT = os.path.join(BASE_DIR, '/static')。这应该有效。 strip 剥离 / 但它需要 /static
  • @doniyor 现在:django.core.exceptions.ImproperlyConfigured: The STATICFILES_DIRS setting should not contain the STATIC_ROOT setting
  • @VictorPolevoy STATIC_ROOT = os.path.join(BASE_DIR, 'sitestatic') 在更新 STATIC_ROOT 变量后运行 collectstatic

标签: python django collectstatic


【解决方案1】:

STATICFILES_DIRS 中的文件需要有绝对路径。 使用规范路径。

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

另外最好将 STATIC_ROOT 设置为类似

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

和 STATIC_URL

STATIC_URL = '/static/'

【讨论】:

    【解决方案2】:

    试试这个:

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

    并将其留空,因为您使用的是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.
    )
    

    它应该以这种方式工作。

    【讨论】:

    • 不幸的是,我首先尝试了@fragles 的解决方案,所以我只能对你的答案投赞成票。
    【解决方案3】:

    所以我遇到了类似的问题并遵循了 fraggles 的建议,但我收到了以下后续错误:

    NameError: name 'normpath' is not defined
    

    我的解决方法是将“.dirname”关键字改为“.normpath”关键字:

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

    它就像魔术一样工作。希望这个解决方案也能帮助别人。

    【讨论】:

      【解决方案4】:

      就这样吧!

      STATIC_URL = '/static/'
      STATICFILES_DIRS = [
          os.path.join(BASE_DIR, 'static'),
      ]
      STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
      
      • 然后运行 ​​python manage.py collectstatic
      • 这将创建一个新文件 staticfiles 并将所有静态文件移到其中。
      • 然后在你的 head 标签中使用 {% load static %} 在你的 django 模板中加载静态

      【讨论】:

        猜你喜欢
        • 2018-09-14
        • 2016-02-15
        • 1970-01-01
        • 2021-06-24
        • 2014-01-08
        • 2019-01-22
        • 1970-01-01
        • 2015-02-20
        相关资源
        最近更新 更多