【发布时间】:2018-11-07 22:28:11
【问题描述】:
我无法在生产中加载静态文件,而它们在本地机器上工作。
我已按照https://devcenter.heroku.com/articles/django-assets 中的步骤进行操作 以及现有的答案,但我无法让它运行。我忽略了一些事情,并希望得到社区的帮助。
这里是部分settings.py:
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
ALLOWED_HOSTS = ['tryml.herokuapp.com','localhost']
# Application definition
INSTALLED_APPS = [
'web.apps.WebConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
MIDDLEWARE = [
'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',
]
ROOT_URLCONF = 'tryml.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'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 = 'tryml.wsgi.application'
# Database
# https://docs.djangoproject.com/en/2.0/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/2.0/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/2.0/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
import dj_database_url
db_from_env = dj_database_url.config(conn_max_age=500)
DATABASES['default'].update(db_from_env)
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
STATIC_ROOT = os.path.join(BASE_DIR, "staticfiles")
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", "tryml.settings")
application = get_wsgi_application()
application = DjangoWhiteNoise(application)
这是试图访问静态内容的 html 文件:
{% extends "web/base.html" %}
{% load static %}
{% block content %}
<div>
<img src="{% static 'web/img/target.jpg' %}" alt="Image could not be loaded"></img>
</div>
{% endblock %}
这是项目结构:
tryml
--static
--web
--img
--target.jpg
--tryml
--settings.py
--wsgi.py
--urls.py
--web(web app)
--(urls,forms,templates,...)
--manage.py
--Procfile
--requirements.txt
--runtime.txt
另一个观察结果是,当我检查从浏览器发送的请求时,它显示静态文件(图像)的 404 错误并说它试图在此处查找- ...herokuapp.com/static/web/img/目标.jpg
不应该从 STATIC_ROOT(staticfiles/web/img/target) 引用吗?
编辑——当代码被推送到heroku时,collectstatic命令运行,它显示了一个成功的复制步骤到app/staticfiles。不过,静态文件没有加载。
EDIT2--我运行了命令 python manage.py findstatic web/img/target.jpg,如下所述:https://docs.djangoproject.com/en/2.0/ref/contrib/staticfiles/#findstatic
它返回本地计算机上的文件位置,但在 heroku 上,它显示“没有找到与 'web/img/target.jpg' 匹配的文件。”为什么即使在 collectstatic 命令成功运行后也找不到它? 静态文件的生产获取实际上是如何工作的?当我使用 {% static web/img/target.jpg %} 时,它会去 STATIC_ROOT 位置获取它吗?
【问题讨论】:
-
那里的 OP 说静态文件工作正常,gzip 存在问题。就我而言,静态文件不会自行加载。你认为它们有关系吗?
-
是的。在您的标题中包含“Accept-Encoding: gzip”之前,Gzip 将无法工作。相反,请尝试 whitenoise.storage.CompressedManifestStaticFilesStorage
-
我的请求标头中已经出现“Accept-Encoding: gzip, deflate, br”。通过更改上述设置,我仍然得到相同的结果..
标签: django heroku django-staticfiles