当Debug = False时,Heroku服务器错误(500),whitenoise找不到style.css

时间:2017-07-29 15:42:49

标签: django heroku

当debug设置为false时,我收到服务器错误(500),但是当debug = True

时,该站点正常工作

这是Heroku日志: [编辑:在DJANGO日志中更改后的新日志文件]

2017-07-30T17:19:06.000000+00:00 app[api]: Build succeeded
2017-07-30T17:19:25.874517+00:00 heroku[web.1]: State changed from up to starting
2017-07-30T17:19:25.873834+00:00 heroku[web.1]: Restarting
2017-07-30T17:19:26.809034+00:00 heroku[web.1]: Stopping all processes with SIGTERM
2017-07-30T17:19:26.828322+00:00 app[web.1]: [2017-07-30 17:19:26 +0000] [11] [INFO] Worker exiting (pid: 11)
2017-07-30T17:19:26.842100+00:00 app[web.1]: [2017-07-30 17:19:26 +0000] [10] [INFO] Worker exiting (pid: 10)
2017-07-30T17:19:26.852227+00:00 app[web.1]: [2017-07-30 17:19:26 +0000] [4] [INFO] Handling signal: term
2017-07-30T17:19:26.868822+00:00 app[web.1]: [2017-07-30 17:19:26 +0000] [4] [INFO] Shutting down: Master
2017-07-30T17:19:27.014793+00:00 heroku[web.1]: Process exited with status 0
2017-07-30T17:19:30.765424+00:00 heroku[web.1]: Starting process with command `gunicorn techzu.wsgi --log-file -`
2017-07-30T17:19:33.522804+00:00 app[web.1]: [2017-07-30 17:19:33 +0000] [4] [INFO] Starting gunicorn 19.7.1
2017-07-30T17:19:33.523786+00:00 app[web.1]: [2017-07-30 17:19:33 +0000] [4] [INFO] Using worker: sync
2017-07-30T17:19:33.523653+00:00 app[web.1]: [2017-07-30 17:19:33 +0000] [4] [INFO] Listening at: http://0.0.0.0:43129 (4)
2017-07-30T17:19:33.528197+00:00 app[web.1]: [2017-07-30 17:19:33 +0000] [10] [INFO] Booting worker with pid: 10
2017-07-30T17:19:33.549206+00:00 app[web.1]: [2017-07-30 17:19:33 +0000] [11] [INFO] Booting worker with pid: 11
2017-07-30T17:19:34.484665+00:00 heroku[web.1]: State changed from starting to up
2017-07-30T17:20:18.368438+00:00 heroku[router]: at=info method=GET path="/" host=www.tecbux.com request_id=84831c2e-e908-4313-8ea0-3a689a54e3a4 fwd="49.207.184.93" dyno=web.1 connect=0ms service=134ms status=500 bytes=239 protocol=http
2017-07-30T17:20:18.362963+00:00 app[web.1]: Traceback (most recent call last):
2017-07-30T17:20:18.362946+00:00 app[web.1]: Internal Server Error: /
2017-07-30T17:20:18.362964+00:00 app[web.1]:   File "/app/.heroku/python/lib/python2.7/site-packages/django/core/handlers/exception.py", line 42, in inner
2017-07-30T17:20:18.362965+00:00 app[web.1]:     response = get_response(request)
2017-07-30T17:20:18.362965+00:00 app[web.1]:   File "/app/.heroku/python/lib/python2.7/site-packages/django/core/handlers/base.py", line 217, in _get_response
2017-07-30T17:20:18.362967+00:00 app[web.1]:     response = self.process_exception_by_middleware(e, request)
2017-07-30T17:20:18.362967+00:00 app[web.1]:   File "/app/.heroku/python/lib/python2.7/site-packages/django/core/handlers/base.py", line 215, in _get_response
2017-07-30T17:20:18.362968+00:00 app[web.1]:     response = response.render()
2017-07-30T17:20:18.362969+00:00 app[web.1]:   File "/app/.heroku/python/lib/python2.7/site-packages/django/template/response.py", line 109, in render
2017-07-30T17:20:18.362969+00:00 app[web.1]:     self.content = self.rendered_content
2017-07-30T17:20:18.362972+00:00 app[web.1]:     return self.template.render(context)
2017-07-30T17:20:18.362970+00:00 app[web.1]:   File "/app/.heroku/python/lib/python2.7/site-packages/django/template/response.py", line 86, in rendered_content
2017-07-30T17:20:18.362971+00:00 app[web.1]:     content = template.render(context, self._request)
2017-07-30T17:20:18.362971+00:00 app[web.1]:   File "/app/.heroku/python/lib/python2.7/site-packages/django/template/backends/django.py", line 66, in render
2017-07-30T17:20:18.362975+00:00 app[web.1]:     return self.nodelist.render(context)
2017-07-30T17:20:18.362973+00:00 app[web.1]:   File "/app/.heroku/python/lib/python2.7/site-packages/django/template/base.py", line 208, in render
2017-07-30T17:20:18.362973+00:00 app[web.1]:     return self._render(context)
2017-07-30T17:20:18.362974+00:00 app[web.1]:   File "/app/.heroku/python/lib/python2.7/site-packages/django/template/base.py", line 199, in _render
2017-07-30T17:20:18.362976+00:00 app[web.1]:   File "/app/.heroku/python/lib/python2.7/site-packages/django/template/base.py", line 961, in render_annotated
2017-07-30T17:20:18.362978+00:00 app[web.1]:   File "/app/.heroku/python/lib/python2.7/site-packages/django/templatetags/static.py", line 104, in render
2017-07-30T17:20:18.362975+00:00 app[web.1]:   File "/app/.heroku/python/lib/python2.7/site-packages/django/template/base.py", line 994, in render
2017-07-30T17:20:18.362978+00:00 app[web.1]:     url = self.url(context)
2017-07-30T17:20:18.362976+00:00 app[web.1]:     bit = node.render_annotated(context)
2017-07-30T17:20:18.362980+00:00 app[web.1]:     return self.handle_simple(path)
2017-07-30T17:20:18.362977+00:00 app[web.1]:     return self.render(context)
2017-07-30T17:20:18.362981+00:00 app[web.1]:     return staticfiles_storage.url(path)
2017-07-30T17:20:18.362979+00:00 app[web.1]:   File "/app/.heroku/python/lib/python2.7/site-packages/django/templatetags/static.py", line 101, in url
2017-07-30T17:20:18.362981+00:00 app[web.1]:   File "/app/.heroku/python/lib/python2.7/site-packages/django/templatetags/static.py", line 114, in handle_simple
2017-07-30T17:20:18.362982+00:00 app[web.1]:   File "/app/.heroku/python/lib/python2.7/site-packages/django/contrib/staticfiles/storage.py", line 132, in url
2017-07-30T17:20:18.362983+00:00 app[web.1]:     hashed_name = self.stored_name(clean_name)
2017-07-30T17:20:18.362983+00:00 app[web.1]:   File "/app/.heroku/python/lib/python2.7/site-packages/django/contrib/staticfiles/storage.py", line 292, in stored_name
2017-07-30T17:20:18.362984+00:00 app[web.1]:     cache_name = self.clean_name(self.hashed_name(name))
2017-07-30T17:20:18.362985+00:00 app[web.1]:   File "/app/.heroku/python/lib/python2.7/site-packages/django/contrib/staticfiles/storage.py", line 95, in hashed_name
2017-07-30T17:20:18.362985+00:00 app[web.1]:     (clean_name, self))
2017-07-30T17:20:18.362988+00:00 app[web.1]: ValueError: The file 'style.css' could not be found with <whitenoise.storage.CompressedManifestStaticFilesStorage object at 0x7f6decd24b50>.

起初我有一个内部服务器错误,但那是因为我没有把我的SENDGRID API放在我的settings.py中,在添加之后我得到了SERVER ERROR(500)

这是 settings.py

EMAIL_HOST = 'smtp.sendgrid.net'
EMAIL_HOST_USER = '****'
EMAIL_HOST_PASSWORD = '****'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_BACKEND = "sgbackend.SendGridBackend"
SENDGRID_API_KEY = "***"

ALLOWED_HOSTS = ['www.xxx.com', 'xxx.com']

MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'whitenoise.middleware.WhiteNoiseMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
 ###
 ]

     DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}

import dj_database_url

db_from_env = dj_database_url.config()
DATABASES['default'].update(db_from_env)

MEDIA_URL = '/media/'
CKEDITOR_UPLOAD_PATH = "uploads/"
CKEDITOR_IMAGE_BACKEND = "pillow"
CKEDITOR_JQUERY_URL = '//code.jquery.com/jquery-3.1.1.min.js'
STATIC_ROOT = os.path.join(PROJECT_ROOT, 'staticfiles')
STATIC_URL = '/static/'

STATICFILES_DIRS = (
    os.path.join(PROJECT_ROOT, 'static'),
)
STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'

MEDIA_ROOT = os.path.join(PROJECT_ROOT, 'static/media')

静态文件加载在Debug = False

这是我尝试 heroku本地网络

的时候
 [WARN] No ENV file found
    22:19:44 web.1   |  [2017-07-29 22:19:44 +0000] [7476] [INFO] Starting gunicorn 19.7.1
    22:19:44 web.1   |  [2017-07-29 22:19:44 +0000] [7476] [INFO] Listening at: http://0.0.0.0:5000 (7476)
    22:19:44 web.1   |  [2017-07-29 22:19:44 +0000] [7476] [INFO] Using worker: sync
    22:19:44 web.1   |  [2017-07-29 22:19:44 +0000] [7482] [INFO] Booting worker with pid: 7482

我得到了一个不好的请求(400)

PS:所以,有些图片也没有加载,这可能是静态文件的问题吗?但我的CSS加载了一些其他图像。

[编辑]未加载的图像是我在django的管理页面上传时上传的图像。在git push heroku master之后,图片没有加载。

这是我的 wsgi.py

import os

from django.core.wsgi import get_wsgi_application

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "techzu.settings")

application = get_wsgi_application()

from whitenoise.django import DjangoWhiteNoise
application = DjangoWhiteNoise(application)

[编辑:ValueError:无法找到'style.css'文件。]

6 个答案:

答案 0 :(得分:4)

settings.py

中添加了此内容
LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'handlers': {
        'console': {
            'class': 'logging.StreamHandler',
        },
    },
    'loggers': {
        'django': {
            'handlers': ['console'],
             'level': os.getenv('DJANGO_LOG_LEVEL', 'DEBUG'),
        },
    },
}

在日志中收到错误 显然,远程服务器和本地计算机中文件夹的名称不匹配

答案 1 :(得分:3)

关闭调试时。 您需要在列表中提供ALLOWED_HOSTS。请查看Django文档了解更多..

 Debug = False
ALLOWED_HOSTS = ['xyz.com']

答案 2 :(得分:2)

你不能在Heroku上使用SQLite3。切换到Postgres。

答案 3 :(得分:2)

尝试通过以下方式进行迁移:

heroku运行python manage.py makemigrations

heroku运行python manage.py迁移

我遇到了同样的问题,但这解决了。

答案 4 :(得分:1)

您需要提供ALLOWED_HOSTS

Debug = False
ALLOWED_HOSTS = [".herokuapp.com"]

和Heroku你应该使用PostgreSQL

https://github.com/kennethreitz/dj-database-url

答案 5 :(得分:0)

就我而言,这是由于之间的冲突 django staticfilesdjango _heroku staticfiles

我不得不禁用其中之一。

要么这样做,

 ` INSTALLED_APPS={
            .
            .
            #django.contrib.staticfiles,
  }
  django_heroku.settings(locals())` #in end of settings.py

或者这样做

    `INSTALLED_APPS={
            .
            .
            django.contrib.staticfiles,
      }
     django_heroku.settings(locals(),staticfiles=False)` #in end of settings.py

或者,如果您正在使用 whitenoise ,请禁用 django 和 django_heroku 的静态文件。