2017-04-13 82 views
0

我想使用Heroku's documentation在我的本地機器和生產環境中提供靜態文件。然而,每當我運行我的應用程序debug=True一切都按預期工作;靜態文件將被檢索並且應用按預期顯示。但是,每當我更換debug=False時,我都會收到Server Error (500)。據我可以告訴這一切歸結到我的STATICFILE_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'。當我發表評論時,我的應用程序將以debug=False運行,但由於缺少靜態文件而沒有樣式。我已經通過了whitenoise documentation,heroku'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:

因此,原來我不得不跑heroku config:unset DISABLE_COLLECTSTATIC獲得Heroku的自動collectstatic。現在,它扔我這些錯誤:

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

這是奇怪的,因爲我可以成功在本地運行的python manage.py collectstatic ...

更新#2

我跑collectstatic本地和一個名爲「staticfiles」的目錄是由我的所有靜態文件由app內部組織的。我推送到heroku,現在我的網站打開,所有靜態文件運行debug=False。我仍然無法讓heroku在沒有出錯的情況下自動收集靜態數據。

+0

什麼是從日誌完整的錯誤? –

+0

如果你使用whitenoise,你不需要cdn,我一直在Heroku的靜態文件中使用它。你有任何錯誤日誌信息? – dentemm

+0

我在上面的第一個問題中添加了日誌。 – grigs

回答

0

所以事實證明,我的設置目錄有很多問題。在我的設置目錄中,我有一個本地和一個生產設置文件。因此,我不得不調整我的BASE_DIR和我的PROJECT_ROOT目錄。我添加了PROJECT_ROOT2 dir以便讓heroku找到我的本地靜態文件。之後,我不得不將空白的css文件放入所需的settings/static目錄,因爲git無法讀取空目錄。畢竟,我終於能夠在本地和heroku上運行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' 
相關問題