简体   繁体   English

Django collectstatic 没有那个文件或目录

[英]Django collectstatic no such file or directory

In django 1.7 collectstatic throws an exception for me:在 django 1.7 collectstatic 为我抛出异常:

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.我执行了很多collectstatic -calls 并且一切正常,但今天有这个问题。

settings.py设置.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. BASE_DIR 是正确的,检查过它。 Directory BASE_DIR/static/ exists and all my static files are there.目录 BASE_DIR/static/ 存在,我所有的 static 文件都在那里。

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?有什么建议么?

try this: 试试这个:

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

and leave this empty, since you are using STATIC_ROOT 并将此保留为空,因为您正在使用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. 它应该这样工作。

Files in STATICFILES_DIRS need to have absolute path. STATICFILES_DIRS中的文件需要具有绝对路径。 Use normpath. 使用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设置为类似的东西

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

and STATIC_URL 和STATIC_URL

STATIC_URL = '/static/'

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: 我的工作是切换'.normpath'关键字的'.dirname'关键字:

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

And it worked like magic. 它像魔术一样运作。 Hopefully this solution also helps someone. 希望这个解决方案也可以帮助某人。

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然后运行python manage.py collectstatic
  • this will create a new file staticfiles and will move all your static files inside it.这将创建一个新文件staticfiles并将在其中移动所有 static 文件。
  • after that load static in your django template by using {% load static %} inside your head tag在你的 django 模板中加载 static 通过在你的 head 标签中使用{% load static %}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM