Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在heroku上使用whitenoise的Django静态文件_Django_Python 3.x_Heroku_Django Staticfiles - Fatal编程技术网

在heroku上使用whitenoise的Django静态文件

在heroku上使用whitenoise的Django静态文件,django,python-3.x,heroku,django-staticfiles,Django,Python 3.x,Heroku,Django Staticfiles,我正在尝试在本地机器和生产环境中使用静态文件。然而,每当我用debug=True运行我的应用程序时,一切都按预期运行;检索静态文件,应用程序按预期显示。但是,每当我更改debug=False时,就会出现服务器错误(500)。据我所知,这一切都归结为我的静态文件存储='whitenoise.django.gzip静态文件存储'。当我对此进行注释时,我的应用程序将以debug=False运行,但由于缺少静态文件,因此没有样式设置。我已经看过了,但是我不知道发生了什么,除了STATICFILE\u存储

我正在尝试在本地机器和生产环境中使用静态文件。然而,每当我用
debug=True
运行我的应用程序时,一切都按预期运行;检索静态文件,应用程序按预期显示。但是,每当我更改
debug=False
时,就会出现
服务器错误(500)
。据我所知,这一切都归结为我的
静态文件存储='whitenoise.django.gzip静态文件存储'
。当我对此进行注释时,我的应用程序将以
debug=False
运行,但由于缺少静态文件,因此没有样式设置。我已经看过了,但是我不知道发生了什么,除了
STATICFILE\u存储
是个问题。白噪音不是用于生产的吗?我必须在生产中使用CDN吗?这是一个小时间的应用程序,所以我希望不必使用CDN,但如果必要的话会使用

settings.py(我只包括我认为相关的settings.py部分。如果您需要全部内容,请告诉我。)

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
提前感谢您的帮助

更新:

所以我不得不运行
heroku config:unset DISABLE\u COLLECTSTATIC
来让heroku自动收集静态。现在它给了我这些错误:

remote:  !     Error while running '$ python manage.py collectstatic --noinput'.
remote:        See traceback above for details.
这很奇怪,因为我可以成功地在本地运行python manage.py collectstatic

更新#2


我在本地运行了
collectstatic
,创建了一个名为“staticfiles”的目录,其中包含了应用程序组织的所有静态文件。我推到heroku,现在我的站点打开,所有静态文件都使用
debug=False
。我仍然无法让heroku自动收集静态数据而不出错

所以我的设置目录与这些问题有很大关系。在我的设置目录中,我有一个本地和生产设置文件。结果,我不得不调整我的
BASE\u DIR
和我的
PROJECT\u ROOT
DIR。为了让heroku找到我的本地静态文件,我添加了
PROJECT\u ROOT2
dir。在那之后,我不得不将一个空白css文件放入所需的settings/static目录,因为git无法读取空目录。在所有这些之后,我终于能够让collectstatic在本地和heroku上运行。这是我更新的设置。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'

日志中的全部错误是什么?如果您使用whitenoise,则不需要cdn,我一直在Heroku上的静态文件中使用它。你有任何错误日志信息吗?我在上面的初始问题中添加了日志。
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'