Heroku Django出现“OSError: [Errno 2] No such file or directory: '/static/static'”错误。

3
我刚接触django和heroku。在将我的git仓库部署到heroku时,遇到了以下错误。请建议我需要进行哪些更改。提前感谢您的帮助。
(网站) C:\Users\website\src>heroku run python manage.py collectstatic --noinput 正在运行python manage.py collectstatic --noinput 命令... 已连接终端,up, run.1690
  Traceback (most recent call last):
  File "manage.py", line 10, in <module>
   execute_from_command_line(sys.argv)
  File "/app/.heroku/python/lib/python2.7/site-packages/django/core/management/__init__
.py", line 385, in execute_from_command_line
utility.execute()
File "/app/.heroku/python/lib/python2.7/site-packages/django/core/management/__init__
.py", line 377, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/app/.heroku/python/lib/python2.7/site-packages/django/core/management/base.py"
, line 288, in run_from_argv
self.execute(*args, **options.__dict__)
File "/app/.heroku/python/lib/python2.7/site-packages/django/core/management/base.py"
, line 338, in execute
output = self.handle(*args, **options)
 File "/app/.heroku/python/lib/python2.7/site-packages/django/core/management/base.py"
, line 533, in handle
return self.handle_noargs(**options)
File "/app/.heroku/python/lib/python2.7/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 168, in handle_noargs
collected = self.collect()
File "/app/.heroku/python/lib/python2.7/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 98, in collect
for path, storage in finder.list(self.ignore_patterns):
File "/app/.heroku/python/lib/python2.7/site-packages/django/contrib/staticfiles/finders.py", line 111, in list
for path in utils.get_files(storage, ignore_patterns):
 File "/app/.heroku/python/lib/python2.7/site-packages/django/contrib/staticfiles/utils.py", line 27, in get_files
directories, files = storage.listdir(location)
 File "/app/.heroku/python/lib/python2.7/site-packages/django/core/files/storage.py",
line 270, in listdir
for entry in os.listdir(path):
 OSError: [Errno 2] No such file or directory: '/static/static'

settings.py

   STATIC_URL = '/static/'

  # Template location

 TEMPLATE_DIRS = (
      os.path.join(os.path.dirname(BASE_DIR), "static", "templates" ),

    )


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

   )

你应该发布关于 STATIC_ROOT、STATIC_URL 或 STATICFILES_DIRS 的 settings.py 部分。 - David Dahan
2个回答

2
如果您已经将您的 'STATICFILES_DIRS' 变量声明如下:
STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'static'),
]

确保:

  1. 存在一个静态文件夹(该文件夹必须以您在“STATICFILES_DIRS”变量中指定的名称命名)。
  2. 它包含一个文件。如果您还没有文件,可以将虚拟文件命名为“.any”。

1

目前看来,错误来自于STATICFILES_DIRS变量指向一个不存在的目录:

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

前往您的Django项目目录,找到静态资源库并记录绝对位置。然后,在settings.py中编辑STATICFILES_DIRS变量,使其指向此位置。
对于我来说(这并不意味着对您也是如此),它看起来像这样:
STATICFILES_DIRS = (os.path.join(BASE_DIR, '../myapp/static'),)

错误已更改为:OSError:[Errno 2]没有此文件或目录:'/myapp/static'这是我尝试将git推送到Heroku时出现的错误准备静态资产 收集静态配置错误。 要进行调试,请运行: $ heroku run python ./manage.py collectstatic --noinput - StackExchange
但是我没有告诉你要写“myapp/static”,那只是我所做的一个例子,但我想你的应用程序不叫“myapp”!你必须找到要写什么,找到正确的位置。 - David Dahan
1
我的应用程序名称是“myapp”。 - StackExchange

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接