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'

EDIT3:

はい !それは動作します^^問題は私のurls.pyにありました。私が書いた :

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

それ以外の

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

エラーは私のメッセージにはありませんでした..はい、私を助けるのは本当に難しかったです. (ごめんなさい) バカみたいです^^'

4

3 に答える 3

15

問題は、使用している絶対パスがSTATIC_ROOTHeroku サーバーに見つからないことです。

それを解決するには、次のアプローチを検討してください。

あなたの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 に答える