Django:收集静态WinError 3

时间:2014-06-23 08:43:06

标签: python django

我在这里遇到了问题。当我尝试" collectstatic"来自manage.py它只是让我关注以下错误:

Traceback (most recent call last):
File "C:\Program Files (x86)\JetBrains\PyCharm 3.1.3\helpers\pycharm\django_manage.py",     line 23, in <module>
run_module(manage_file, None, '__main__', True)
File "C:\Python34\lib\runpy.py", line 182, in run_module
return _run_module_code(code, init_globals, run_name, mod_spec)
File "C:\Python34\lib\runpy.py", line 96, in _run_module_code
mod_name, mod_spec, pkg_name, script_name)
File "C:\Python34\lib\runpy.py", line 85, in _run_code
exec(code, run_globals)
File "C:/Users/mszalewski/PycharmProjects/OwnPixel_com\manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "C:\Python34\lib\site-packages\django\core\management\__init__.py", line 399, in execute_from_command_line
utility.execute()
File "C:\Python34\lib\site-packages\django\core\management\__init__.py", line 392, in  execute
 self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Python34\lib\site-packages\django\core\management\base.py", line 242, in    run_from_argv
    self.execute(*args, **options.__dict__)
 File "C:\Python34\lib\site-packages\django\core\management\base.py", line 285, in execute
output = self.handle(*args, **options)
File "C:\Python34\lib\site-packages\django\core\management\base.py", line 415, in handle
return self.handle_noargs(**options)
File "C:\Python34\lib\site-packages\django\contrib\staticfiles\management\commands\collectstatic.py", line 173, in handle_noargs
collected = self.collect()
File "C:\Python34\lib\site-packages\django\contrib\staticfiles\management\commands\collectstatic.py", line 103, in collect
for path, storage in finder.list(self.ignore_patterns):
File "C:\Python34\lib\site-packages\django\contrib\staticfiles\finders.py", line 106, in list
for path in utils.get_files(storage, ignore_patterns):
File "C:\Python34\lib\site-packages\django\contrib\staticfiles\utils.py", line 25, in get_files
directories, files = storage.listdir(location)
File "C:\Python34\lib\site-packages\django\core\files\storage.py", line 250, in listdir
for entry in os.listdir(path):
FileNotFoundError: [WinError 3] Das System kann den angegebenen Pfad nicht finden: 'C:\\Users\\mszalewski\\PycharmProjects\\static\\static'

Process finished with exit code 1

以下是我的settings.py:

import os
import os.path
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
DEBUG = True
TEMPLATE_DEBUG = True
ALLOWED_HOSTS = []
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
)

MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
)

ROOT_URLCONF = 'OwnPixel_com.urls'

WSGI_APPLICATION = 'OwnPixel_com.wsgi.application'
DATABASES = {
'default': {
    'ENGINE': 'django.db.backends.sqlite3',
    'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True

STATIC_URL = '/static/'

TEMPLATE_DIRS = (
os.path.join(os.path.dirname(BASE_DIR), "static", "templates").replace('\\','/')
)

if DEBUG:
MEDIA_URL = '/media/'
STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), "static", "static-only").replace('\\', '/')
MEDIA_ROOT = os.path.join(os.path.dirname(BASE_DIR), "static", "media").replace('\\', '/')
STATICFILES_DIRS = (
    os.path.join(os.path.dirname(BASE_DIR), "static", "static").replace('\\', '/'),
)

至少我的extendes urls.py

if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL,
                        document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL,
                        document_root=settings.MEDIA_ROOT)

我非常肯定它是路径中的双反斜杠(在错误代码的末尾)。

我希望你能帮我解决问题。

感谢!!!

问候,Marvyn

2 个答案:

答案 0 :(得分:2)

您的目录结构实际上是什么样的?

我怀疑&#39; C:\ Users \ mszalewski \ PycharmProjects \ static \ static&#39;是您打算用于特定于项目的静态文件的文件夹。

我的猜测是你要改变

os.path.join(os.path.dirname(BASE_DIR), "static", "static").replace('\\', '/')

os.path.join(BASE_DIR, "static", "static")

或类似的东西。并且不要担心&#39; \\&#39;和&#39; /&#39;。 Python实际上可以很好地与它们在同一条路径中的一些非常奇怪的组合相处。

答案 1 :(得分:0)

因为您在虚拟目录中(假设您已经如此激活虚拟环境:source <*Scripts* or *bin*>/activate),STATIC_ROOTSTATICFILES_DIRS所期望的路径会有所不同。

1)STATIC_ROOT期待本地计算机上静态文件的位置(无论是您正在开发的计算机上为这些文件提供服务的云服务器) - &gt;即STATIC_ROOT = 'C:/Users/<user>/Desktop/django_env_folder/mysite/static/mysite'

之类的东西

2)另一方面,STATICFILES_DIRS期待virtual_env路径(请记住,当您使用上面的命令激活虚拟环境时,os.<method>()所需的路径是不同的(进行一些调试以查看,尝试运行os.listdir('C:/....'),你会发现不同于预期的结果。所以尝试这样的STATICFILES_DIRS - &gt; STATICFILES_DIRS = [ 'mysite/static/mysite', ]

**此解决方案在localhost投放时绝对有效,请确保Debug = True中的settings.py