heroku django collectstatic错误

时间:2015-05-07 08:12:29

标签: django heroku

我正在尝试在heroku上运行收集我的静态文件,所以我运行了命令:

heroku run python manage.py collectstatic --noinput

我的设置文件是:

BASE_DIR = os.path.dirname(os.path.abspath(__file__))   
STATIC_ROOT = 'staticfiles'
STATIC_URL = '/static/'

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'static'),
)
# define the directory which all upload files will be saved
PROJECT_ROOT = os.path.abspath(os.path.dirname(os.path.dirname(__file__)))
MEDIA_ROOT = os.path.join(PROJECT_ROOT, 'MyApp/media/')
MEDIA_URL = '/MyApp/media/'

我收到以下错误消息:

OSError: [Errno 2] No such file or directory: '/app/MyApp/static'
这是什么意思?

为什么我应该在“/ app / MyApp”中有一个“静态”文件?不应该是

STATIC_ROOT?

完全追溯:

Running `python manage.py collectstatic --noinput` attached to terminal... up, r

un.9966

Traceback (most recent call last):
  File "manage.py", line 11, in <module>
      execute_from_command_line(sys.argv)
  File "/app/.heroku/python/lib/python2.7/sitepackages/django/core/management/_
_init__.py", line 385, in execute_from_command_line
    utility.execute()
  File "/app/.heroku/python/lib/python2.7/sitepackages/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/sitepackages/django/core/management/base.py",
line 288, in run_from_argv
    self.execute(*args, **options.__dict__)
  File     "/app/.heroku/python/lib/python2.7/sitepackages/django/core/management/base.py",     line 338, in execute
    output = self.handle(*args, **options)
  File "/app/.heroku/python/lib/python2.7/sitepackages/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/sitepackages/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/sitepackages/django/contrib/staticfiles/utils.py", line 27, in get_files
directories, files = storage.listdir(location)
      File "/app/.heroku/python/lib/python2.7/sitepackages/django/core/files/storage.py",     line 270, in listdir
for entry in os.listdir(path):
OSError: [Errno 2] No such file or directory: '/app/CTFdj/static'

编辑: OSError:[Errno 2]没有这样的文件或目录:'/ app / CTFdj / static'=&gt;&gt;

OSError:[Errno 2]没有这样的文件或目录:'/ app / MyApp / static'

2 个答案:

答案 0 :(得分:1)

我怀疑您收到了该错误,因为您的项目结构未正确设置BASE_DIR。尝试在本地运行python manage.py collectstatic --noinput以查看是否收到相同的错误。您还可以在print(BASE_DIR)settings.py检查它是否指向正确的位置,以及static文件夹实际位于该目录中。对于我的项目,我有几个级别的设置文件,但static目录位于项目的根目录,所以我的设置如下所示:

BASE_DIR = os.path.dirname(os.path.dirname(os.path.dirname(__file__))) // base_dir/subdir/settings/settings.py
STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'static'), // base_dir/static
)

另外,对于我的Heroku应用,我加入BASE_DIRstaticfiles一起加入STATIC_ROOT,如下所示:

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

另一方面,我应警告您,您无法将媒体文件直接上传到Heroku应用,就像您目前使用MEDIA_ROOT进行设置一样。 Heroku使用ephemeral filesystem来丢弃dyno停止或重新启动后写入的文件。您需要使用Amazon S3等第三方服务上传用户提交的媒体文件。 django-storages应用可以帮助您进行整合。

答案 1 :(得分:0)

我可能无法回答为什么会看/app/CTFdj/static,但可能的原因之一是因为您的STATIC_ROOT可能配置错误。它应该是the absolute path where the collected static will be copied to。看看我通常如何组织我的STATIC_ROOT。希望这会有所帮助。

相关问题