2017-04-13 74 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' 
相关问题