django中的服务器错误(500)在debug = False时部署在heroky上

时间:2020-03-02 15:05:32

标签: django heroku

当我使用DEBUG = True运行时,一切正常,但是当我设置DEBUG = False时,我只有Server Error 500。我正在使用最新的Django版本(3.x.x),并将我的项目部署到heroku。我不知道为什么会这样。

我的settings.py文件(某些部分)如下

import django_heroku
import os

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = False

ALLOWED_HOSTS = ['research-and-development.herokuapp.com',
                 'localhost', '127.0.0.1']

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

MIDDLEWARE = [
    'corsheaders.middleware.CorsMiddleware',
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'corsheaders.middleware.CorsPostCsrfMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.0/howto/static-files/

# static and media files and urls set-up
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')

MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
# end of static and media files and url set-up

django_heroku.settings(locals())

请帮助我解决这个问题。

1 个答案:

答案 0 :(得分:2)

我不知道这是否是最佳答案。我已经尝试了很多东西。最终,我删除了 django_heroku 。我关注了django heroku文档,而我的settings.py(部分内容)如下。

DEBUG = False

ALLOWED_HOSTS = ['research-and-development1.herokuapp.com',
                 'localhost', '127.0.0.1', '0.0.0.0', ]

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

DATABASES['default'].update(dj_database_url.config(conn_max_age=600, ssl_require=True))

MIDDLEWARE = [
    'whitenoise.middleware.WhiteNoiseMiddleware',
    'corsheaders.middleware.CorsMiddleware',
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'corsheaders.middleware.CorsPostCsrfMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]


# static and media files and urls set-up
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')

MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')

在我推送之前,我已经使用以下命令在heroku上创建了psql数据库。 heroku addons:create heroku-postgresql:hobby-dev和我推送了使用python manage.py collectstatic生成的收集的静态文件。祝您编码愉快。