2017-04-13 24 views
0

Heroku's documentationを使用して、ローカルマシンと本番環境で静的ファイルを提供しようとしています。しかし、いつでも私はdebug=Trueで私のアプリを実行すると、すべて期待どおりに動作します。静的ファイルが取得され、アプリケーションが意図したとおりに表示されます。しかし、私がdebug=Falseを変更するたびに、Server Error (500)が得られます。私が言うことができる限り、それはすべて私のSTATICFILE_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'に沸き立つ。私がそれをコメントアウトすると、私のアプリはdebug=Falseで実行されますが、スタティックファイルがないためスタイリングはありません。私はwhitenoise documentationheroku'sを通過しましたが、何が起こっているのか分からず、STATICFILE_STORAGEから離れています。ホワイトニングは生産のためのものではありませんか?本番環境ではCDNを使用する必要がありますか?それは小さなアプリケーションですので、私はCDNを使用する必要がないことを望んでいましたが、必要に応じて行います。herokuでwhitenoiseを使ったDjangoの静的ファイル

settings.py(私は唯一の私は、関連すると思っていたsettings.pyのパーツが含まれています。あなたは、全体のことが必要な場合は、私に知らせてください。)

import os 


# Build paths inside the project like this: os.path.join(BASE_DIR, ...) 
BASE_DIR = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) 
PROJECT_ROOT = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) 

# SECURITY WARNING: don't run with debug turned on in production! 
DEBUG = False 

MIDDLEWARE = [ 
    'django_hosts.middleware.HostsRequestMiddleware', 
    'django.middleware.security.SecurityMiddleware', 
    'whitenoise.middleware.WhiteNoiseMiddleware', 
    ....... 

# Static files (CSS, JavaScript, Images) 
# https://docs.djangoproject.com/en/1.9/howto/static-files/ 
STATIC_ROOT = os.path.join(PROJECT_ROOT, 'staticfiles') 
STATIC_URL = '/static/' 

# Extra places for collectstatic to find static files. 
STATICFILES_DIRS = (
    os.path.join(PROJECT_ROOT, 'static'), 
) 

# Simplified static file serving. 
# https://warehouse.python.org/project/whitenoise/ 

STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage' 

wsgi.py

import os 

from django.core.wsgi import get_wsgi_application 
from whitenoise.django import DjangoWhiteNoise 

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "MySite.settings") 

application = get_wsgi_application() 
application = DjangoWhiteNoise(application) 

ログ

2017-04-14T08:40:06.800390+00:00 heroku[router]: at=info method=GET path="/" host=wgsite.herokuapp.com request_id=6bf564f7-efba-42fb-b8b1-970a02a5283a fwd="5.51.58.217" dyno=web.1 connect=0ms service=11ms status=302 bytes=223 protocol=https 
2017-04-14T08:40:06.997956+00:00 heroku[router]: at=info method=GET path="/login" host=wgsite.herokuapp.com request_id=4ce4486e-7e06-4fcd-a562-88ec3ffd8fa9 fwd="5.51.58.217" dyno=web.1 connect=0ms service=6ms status=302 bytes=243 protocol=https 

ご協力いただきありがとうございます!

UPDATE:

だから、私は自動collectstaticにHerokuのを取得するためにheroku config:unset DISABLE_COLLECTSTATICを実行する必要がありましたが判明しました。今、そのは私にこれらのエラーを投げる:私は成功を収めてローカルのpython manage.pyのcollectstaticを実行することができますので、奇数である

remote: !  Error while running '$ python manage.py collectstatic --noinput'. 
remote:  See traceback above for details. 

...

UPDATE#2

私はローカルcollectstaticを実行し、 "staticfiles"という名前のディレクトリが作成され、静的なファイルがすべてアプリケーション内部で整理されています。私はherokuにプッシュし、今私のサイトはdebug=Falseで機能するすべての静的ファイルで開きます。私はまだherokuにエラーを出さずに静的に自動的に収集することはできません。

+0

ログからの完全なエラーは何ですか? –

+0

whitenoiseを使用する場合、cdnは必要ありません。私はHerokuのstaticfilesのために常に使用します。エラーログ情報はありますか? – dentemm

+0

上記の最初の質問にログを追加しました。 – grigs

答えて

0

私の設定ディレクトリはこの問題と関係がありました。私の設定ディレクトリには、ローカルとプロダクション設定ファイルがあります。その結果、私はBASE_DIRと私のPROJECT_ROOTディレクトリを調整しなければなりませんでした。私はPROJECT_ROOT2 dirを追加して、herokuがローカルの静的ファイルを見つけるようにしました。その後、gitは空のディレクトリを読み取ることができないので、実際には空のCSSファイルを必要な設定/静的ディレクトリに配置する必要がありました。結局のところ、私は最終的に地元や英雄の上で実行するためにcollectstaticを得ることができました。ここに私の更新されたsettings.py

import os 


# Build paths inside the project like this: os.path.join(BASE_DIR, ...) 
BASE_DIR = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) 
PROJECT_ROOT = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) 
PROJECT_ROOT2 = os.path.dirname(os.path.abspath(__file__)) 

# Quick-start development settings - unsuitable for production 
# See https://docs.djangoproject.com/en/1.10/howto/deployment/checklist/ 

# SECURITY WARNING: don't run with debug turned on in production! 
DEBUG = False 

ALLOWED_HOSTS = ['www.site.co', 'site', 'wgsite.herokuapp.com'] 

# Application definition 

INSTALLED_APPS = [ 
    # Django Apps 
    'django.contrib.admin', 
    'django.contrib.auth', 
    'django.contrib.contenttypes', 
    'django.contrib.sessions', 
    'django.contrib.messages', 
    'django.contrib.staticfiles', 
    # Third Party Apps 
    'crispy_forms', 
    'django.contrib.humanize', 
    'django_hosts', 
    # My Apps 
    'argent', 
    'home', 
    'accounts', 

] 

MIDDLEWARE = [ 
    'django_hosts.middleware.HostsRequestMiddleware', 
    'django.middleware.security.SecurityMiddleware', 
    'whitenoise.middleware.WhiteNoiseMiddleware', 
    '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', 
    'MySite.middleware.LoginRequiredMiddleware', 
    'django_hosts.middleware.HostsResponseMiddleware', 
] 

LOGIN_URL = '/login' 

LOGIN_EXEMPT_URLS = [ 
    '/logout', 
    '/register', 
] 

ROOT_URLCONF = 'MySite.urls' 
ROOT_HOSTCONF = 'MySite.hosts' 
DEFAULT_HOST = 'www' 
DEFAULT_REDIRECT_URL = 'http://www.site.co' 


TEMPLATES = [ 
    { 
     'BACKEND': 'django.template.backends.django.DjangoTemplates', 
     'DIRS': [os.path.join(BASE_DIR, 'templates')] 
     , 
     'APP_DIRS': True, 
     'OPTIONS': { 
      'context_processors': [ 
       'django.template.context_processors.debug', 
       'django.template.context_processors.request', 
       'django.contrib.auth.context_processors.auth', 
       'django.contrib.messages.context_processors.messages', 
      ], 
     }, 
    }, 
] 

WSGI_APPLICATION = 'MySite.wsgi.application' 

# Database 
# https://docs.djangoproject.com/en/1.10/ref/settings/#databases 

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

# Password validation 
# https://docs.djangoproject.com/en/1.10/ref/settings/#auth-password-validators 

AUTH_PASSWORD_VALIDATORS = [ 
    { 
     'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', 
    }, 
    { 
     'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', 
    }, 
    { 
     'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', 
    }, 
    { 
     'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', 
    }, 
] 

# Internationalization 
# https://docs.djangoproject.com/en/1.10/topics/i18n/ 

LANGUAGE_CODE = 'en-us' 

TIME_ZONE = 'CET' 

USE_I18N = True 

USE_L10N = True 

USE_TZ = True 

CRISPY_TEMPLATE_PACK = 'bootstrap3' 


# Static files (CSS, JavaScript, Images) 
# https://docs.djangoproject.com/en/1.9/howto/static-files/ 
STATIC_ROOT = os.path.join(PROJECT_ROOT, 'staticfiles') 
STATIC_URL = '/static/' 

# Extra places for collectstatic to find static files. 
STATICFILES_DIRS = (
    os.path.join(PROJECT_ROOT2, 'static'), 
) 

# Simplified static file serving. 
# https://warehouse.python.org/project/whitenoise/ 

STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage' 
関連する問題