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

3

当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>.

一开始我遇到了内部服务器错误,但那是因为我没有在settings.py文件中添加我的SENDGRID API。添加后,我收到了服务器错误(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 local web时的情况。

 [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”无法使用.找到。]


请提供一些代码。你是否安装了白噪声,静态根已经配置等?列出你的settings.py、gunicorn、urls.py等中所有可能出错的源。 - hansTheFranz
是的。我现在会添加翻译后的文本。 - Sanjeev Siva
@hansTheFranz 已添加所需信息。 - Sanjeev Siva
6个回答

14

将以下内容添加到settings.py文件中:

Added this in the 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'),
        },
    },
}

在日志中发现了错误。 显然远程服务器和我的本地机器上的文件夹名称不匹配。


2
这很有用,无论原来的问题是什么。谢谢。 - kontur

8

尝试使用以下命令进行迁移:

heroku run python manage.py makemigrations

heroku run python manage.py migrate

我曾经遇到过同样的问题,但是通过这种方法解决了。


4
在关闭调试模式时,您需要提供ALLOWED_HOSTS列表。请查看Django文档获取更多信息。
Debug = False
ALLOWED_HOSTS = ['xyz.com']

是的,ALLOWED_HOSTS 已经设置好了,那不是问题。 - Sanjeev Siva

3

我该将哪些部分排除在ALLOWED_HOSTS之外? - Elinaldo Monteiro

3

在我的情况下,这是由于 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 的 staticfiles。


非常感谢!我已经苦恼了很多个小时……但是这个解决方案帮助了我。 - BlindSide

3

在Heroku上无法使用SQLite3,需切换到Postgres。


1
这是一个好建议,但我不认为它会导致HTTP 500错误。 - Chris
导入dj_database_urldb_from_env = dj_database_url.config()DATABASES['default'].update(db_from_env) - Sanjeev Siva

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接