Css 希罗库;django:服务器不';无法加载静态文件
昨晚我决定把我的django学校项目投入生产。为此,我使用了Heroku。一切都很顺利,但“最后一次接触”让我抓狂。它似乎没有在网站上看到我的静态文件,比如bootstrap css,我不知道为什么,因为例如ball.jpg被加载,admin css也被加载。如果有人能发现我在过去10小时里遇到的问题,那将是一个救命稻草:) 你可以看到结果 我的设置.pyCss 希罗库;django:服务器不';无法加载静态文件,css,django,git,heroku,django-staticfiles,Css,Django,Git,Heroku,Django Staticfiles,昨晚我决定把我的django学校项目投入生产。为此,我使用了Heroku。一切都很顺利,但“最后一次接触”让我抓狂。它似乎没有在网站上看到我的静态文件,比如bootstrap css,我不知道为什么,因为例如ball.jpg被加载,admin css也被加载。如果有人能发现我在过去10小时里遇到的问题,那将是一个救命稻草:) 你可以看到结果 我的设置.py from django.conf import settings if not settings.DEBUG: import os imp
from django.conf import settings
if not settings.DEBUG:
import os
import dj_database_url
BASE_DIR = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.8/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '*****'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = False
ALLOWED_HOSTS = ["*"]
# Honor the 'X-Forwarded-Proto' header for request.is_secure()
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
# Application definition
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'crispy_forms',
'team',
'tournament',
'geoposition',
'emailing',
)
MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django.middleware.security.SecurityMiddleware',
)
ROOT_URLCONF = 'football.urls'
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 = 'football.wsgi.application'
# Database
# https://docs.djangoproject.com/en/1.8/ref/settings/#databases
DATABASES = settings.DATABASES
DATABASES['default'] = dj_database_url.config()
# Internationalization
# https://docs.djangoproject.com/en/1.8/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static asset configuration
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, "media", "static_root")
STATICFILES_DIRS = (
os.path.join(BASE_DIR, "media_in_pro", "static"),
)
# Simplified static file serving.
# https://warehouse.python.org/project/whitenoise/
STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'
# MEDIA SETTINGS
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, "media", "media_root")
# CRISPY FORM SETTINGS
CRISPY_TEMPLATE_PACK = 'bootstrap3'
# Django-Geoposition
GEOPOSITION_MAP_OPTIONS = {
'minZoom': 8,
'maxZoom': 15,
'center': {'lat': 53.13248859999999,
'lng': 23.168840300000056 },
}
GEOPOSITION_MAP_WIDGET_HEIGHT = 240
GEOPOSITION_MARKER_OPTIONS = {
'cursor': 'move'
}
在推Git的时候
remote: Compressing source files... done.
remote: Building source:
remote:
remote: -----> Using set buildpack heroku/python
remote: -----> Python app detected
remote: -----> Installing dependencies with pip
remote:
remote: -----> Preparing static assets
remote: Running collectstatic...
remote: Post-processed 'admin/js/admin/DateTimeShortcuts.js' as 'admin/js/admin/DateTimeShortcuts.140919a6a17e.js'
(...)
remote: 82 static files copied to '/app/media/static_root', 82 post-processed.
remote:
remote:
remote: -----> Discovering process types
remote: Procfile declares types -> web
remote:
remote: -----> Compressing... done, 48.6MB
remote: -----> Launching... done, v51
remote: https://footballapp.herokuapp.com/ deployed to Heroku
remote:
remote: Verifying deploy.... done.
我的heroku日志——尾巴:
编辑:
我发现它似乎没有将我的include html加载到base.html,有人有这个问题吗?我最近在Heroku上遇到了类似的问题。Django似乎无法在生产中单独使用静态资产。我可以用它来代替静力学。首先安装它:
$pip安装whitenoise
还要确保将whitenoise
添加到requirements.txt
中
现在将以下内容添加到您的wsgi.py:
from django.core.wsgi import get_wsgi_application
from whitenoise.django import DjangoWhiteNoise
application = get_wsgi_application()
application = DjangoWhiteNoise(application)
最后,在settings.py中配置whitenoise:
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
或者根据您正在运行的Django和WhiteNoise版本:
STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'
另外,请确保在设置.py中正确配置了静力学
PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))
# 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'),
)
更多信息:这是您的第一个heroku应用程序吗?似乎heroku在为第二个应用程序提供静态文件时遇到了一些问题。
PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))
# 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'),
)