将Django部署到Heroku服务器时出错(500)

2024-04-25 08:49:31 发布

您现在位置:Python中文网/ 问答频道 /正文

我一直在制作一个django应用程序,现在正试图将其部署到heroku。 但是当我继续写的时候,上面写着Server Error (500),日志上写着:2017-05-27T21:00:14.634310+00:00 heroku[router]: at=info method=GET path="/" host=remberit.herokuapp.com request_id=065d27c6-9211-458f-9fc6-bb677d43581e fwd="86.13.204.65" dyno=web.1 connect=0ms service=151ms status=500 bytes=387 protocol=https

这是我的settings.py(在学习相关部分时,请询问是否需要其他部分):

PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))

STATIC_ROOT = os.path.join(PROJECT_ROOT, "staticfiles")
STATIC_URL = '/static/'

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'static'),
)

STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'

import dj_database_url

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

SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')

ALLOWED_HOSTS = ['*']

DEBUG = False

try:
    from .local_settings import *
except ImportError:
    pass

这是我的wsgi.py:

import os

from django.core.wsgi import get_wsgi_application
#from whitenoise.django import DjangoWhiteNoise
import django

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'remberit.settings')
django.setup()

application = get_wsgi_application()
#application = DjangoWhiteNoise(application)

这是我的程序文件:

web: gunicorn remberit.wsgi

这是我的runtime.txt:

python-3.5.2

这是我的要求.txt:

appdirs==1.4.3
dj-database-url==0.4.2
gunicorn==19.7.1
packaging==16.8
pyparsing==2.2.0
six==1.10.0
whitenoise==3.3.0
psycopg2==2.6.2

这里是pip freeze的输出:

appdirs==1.4.3
dj-database-url==0.4.2
gunicorn==19.7.1
packaging==16.8
pyparsing==2.2.0
six==1.10.0
whitenoise==3.3.0

另外,当我使用gunicorn remberit.wsgipython manage.py runserver在本地运行应用程序时,它工作得很好,只有当我使用heroku时它才不工作。

如果你需要更多的信息,请告诉我。


Tags: pathdjangopyimporturlwsgiherokusettings
3条回答

正如mustapha-belkacim所说,您需要迁移应用程序:

heroku run python manage.py migrate

请从设置中提供(或检查)模板变量。确保'DIRS'值指向index.html(或基本模板)所在的正确模板文件夹。

我也面临同样的错误,这就是我的问题所在。

正如在另一个问题(Django app deployed to Heroku producing Server error 500)中所评论的那样,我在生产环境中将DEBUG转换为True以找到关于这个问题的更好的帮助之后意识到了这一点。然后,回到False。

你的/管理员工作吗?如果是的话,这个答案可能会对你有帮助。如果不是,有很多事情可能会出错,也许其他人可以帮忙?(例如:如果您在requirements.txt中有psycopg2,为什么它不与pip freeze一起输出?是否已安装?您是否正在本地和生产环境中使用postgreSQL?)

其他可能解决您问题的解决方案:从whitenoise中注释掉或移除gzipmanifeststaticfiles存储。不知什么原因,这不太管用。

# STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'

Why would django fail with server 500 only when Debug=False AND db is set to production database on Heroku?上找到此解决方案

相关问题 更多 >