17

我是heroku的新手,我尝试了一个没有css的简单django应用程序。

但我只是在我的应用程序中添加了一个 css 文件,当我这样做时:

git push heroku master

静态文件收集失败:

[...]
-----> Collecting static files
Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/tmp/build_2unndirli15s7/.heroku/venv/lib/python2.7/site-packages/django/core/management/__init__.py", line 443, in execute_from_command_line
    utility.execute()
  File "/tmp/build_2unndirli15s7/.heroku/venv/lib/python2.7/site-packages/django/core/management/__init__.py", line 382, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/tmp/build_2unndirli15s7/.heroku/venv/lib/python2.7/site-packages/django/core/management/base.py", line 196, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/tmp/build_2unndirli15s7/.heroku/venv/lib/python2.7/site-packages/django/core/management/base.py", line 232, in execute
    output = self.handle(*args, **options)
  File "/tmp/build_2unndirli15s7/.heroku/venv/lib/python2.7/site-packages/django/core/management/base.py", line 371, in handle
    return self.handle_noargs(**options)
  File "/tmp/build_2unndirli15s7/.heroku/venv/lib/python2.7/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 163, in handle_noargs
    collected = self.collect()
  File "/tmp/build_2unndirli15s7/.heroku/venv/lib/python2.7/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 104, in collect
    for path, storage in finder.list(self.ignore_patterns):
  File "/tmp/build_2unndirli15s7/.heroku/venv/lib/python2.7/site-packages/django/contrib/staticfiles/finders.py", line 105, in list
    for path in utils.get_files(storage, ignore_patterns):
  File "/tmp/build_2unndirli15s7/.heroku/venv/lib/python2.7/site-packages/django/contrib/staticfiles/utils.py", line 25, in get_files
    directories, files = storage.listdir(location)
  File "/tmp/build_2unndirli15s7/.heroku/venv/lib/python2.7/site-packages/django/core/files/storage.py", line 235, in listdir
    for entry in os.listdir(path):
OSError: [Errno 2] No such file or directory: '/home/kevin/web/django/appheroku/blogapp/static'
 !     Heroku push rejected, failed to compile Python/django app

To git@heroku.com:[...]
 ! [remote rejected] master -> master (pre-receive hook declined)
error: failed to push some refs to 'git@heroku.com:[...]'

这是我的settings.py:

[...]
STATIC_ROOT = '/home/kevin/web/django/appheroku/staticfiles'

STATIC_URL = '/static/'

STATICFILES_DIRS = (
    '/home/kevin/web/django/appheroku/blogapp/static',
)
[...]

urls.py:

 [...]
 if settings.DEBUG:
   urlpatterns += staticfiles_urlpatterns()

档案:

web: python appheroku/manage.py collectstatic --noinput; bin/gunicorn_django --workers=4 --bind=0.0.0.0:$PORT appheroku/monblog/settings.py

似乎甚至 '/home/kevin/web/django/appheroku/blogapp/static' 目录存在,它没有被检测到,我不知道为什么。:(

请帮帮我^^

编辑:

现在我的 settings.py 看起来像这样:

import os
PROJECT_ROOT = os.path.abspath(os.path.dirname(__file__))
PROJECT_DIR = os.path.join(PROJECT_ROOT,'../blogapp')
[...]
STATIC_ROOT = os.path.join(PROJECT_ROOT,'staticfiles/')
STATIC_URL = '/static/'
STATICFILES_DIRS = (
    os.path.join(PROJECT_DIR,'static/'),
)
[...]

现在静态文件收集步骤似乎运行良好:

 -----> Collecting static files
        74 static files copied.

还有另一个问题:在我的 procfile 中,没有找到 'appheroku/manage.py' 、 'bin/gunicorn_django' 和 'appheroku/monblog/settings.py' 。我修复了它,现在我的 procfile 看起来像这样:

web: python manage.py collectstatic --noinput; gunicorn_django --workers=4 --bind=0.0.0.0:$PORT monblog.settings

该应用程序不再崩溃,但仍然没有 CSS。这是日志:

'2012-05-31T17:35:16+00:00 heroku[router]: GET xxxx-xxxx-number.herokuapp.com/ dyno=web.1 queue=0 wait=0ms service=85ms status=200 bytes=1054
2012-05-31T17:35:17+00:00 heroku[router]: GET xxxx-xxxx-number.herokuapp.com/static/css/style.css dyno=web.1 queue=0 wait=0ms service=5ms status=404 bytes=2088
2012-05-31T17:35:17+00:00 heroku[router]: GET xxxx-xxxx-number.herokuapp.com/static/js/jquery-1.7.2.min.js dyno=web.1 queue=0 wait=0ms service=5ms status=404 bytes=2115'

编辑3:

是的 !它有效^^问题出在我的urls.py中。我写 :

[...]
if not settings.DEBUG:
   urlpatterns += staticfiles_urlpatterns()

代替

 [...]
 if settings.DEBUG:
   urlpatterns += staticfiles_urlpatterns() 

错误不在我的信息中。是的,真的很难帮助我。(对不起)我觉得自己好笨^^'

4

3 回答 3

15

STATIC_ROOT问题是在 Heroku 服务器中找不到您使用的绝对路径。

要解决它,请考虑以下方法。

在您的 settings.py 中:

PROJECT_ROOT = os.path.abspath(os.path.dirname(__file__))
STATIC_ROOT= os.path.join(PROJECT_DIR,'staticfiles/')
STATICFILES_DIRS = (
    os.path.join(PROJECT_ROOT,'static/'),
)
于 2012-05-31T01:20:26.783 回答
3

你正在路上。发生的事情是 Heroku 正在尝试使用您的机器上本地存在但 Heroku 服务器上不存在的 STATICFILES_DIRS 设置 (/home/kevin/web/django/appheroku/blogapp/static)。

一个简单的解决方案是从 staticfiles_dir 变量中删除该行,以便您拥有:

STATICFILES_DIRS = ()

然后您的默认 STATICFILES_FINDERS 将启动,您应该可以在本地运行您的应用程序以及部署在 Heroku 上。

于 2012-05-31T00:06:24.580 回答
1

对于像我这样进来的人:

就像@eliotk 暗示的那样,

PROJECT_ROOT = os.path.abspath(os.path.dirname(__file__))
STATIC_ROOT= os.path.join(PROJECT_DIR,'staticfiles/')
STATICFILES_DIRS = ()

这是您要避免FileNotFoundError: [Errno 2] No such file or directory:错误或OSError: [Errno 30] Read-only file system:错误的配置。

于 2016-06-22T16:39:18.533 回答