Home > Mobile >  Django Admin raise 500 error in production Heroku
Django Admin raise 500 error in production Heroku

Time:12-21

I know this question was raised multiple times, but wasn't able to solve the problem. Here is my problem: my Django-React app is deployed on Heroku and works great (very simple app). I would like know to access the /admin part of my app, but I get a 500 Internal Server Error. The error appears locally and in Heroku. The DEBUG is False, and unfortunately I can't get the logs to work, neither in Heroku nor locally :(

Here is my settings.py:

from pathlib import Path
import django_heroku
import os

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.2/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'xxxxxxxxxxxxxxxxxxxxxx'

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

ALLOWED_HOSTS = ['vmbf.herokuapp.com', '127.0.0.1', 'localhost']


ADMINS = [('username', 'emailaddress')]
MANAGERS = ADMINSEMAIL_HOST = 'host'
SEND_BROKEN_LINK_EMAILS=True
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'

EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'emailaddress'
EMAIL_HOST_PASSWORD = 'password'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
SERVER_EMAIL = EMAIL_HOST_USER


# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',  
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'rest_framework',
    'corsheaders',
    'students',
]

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

ROOT_URLCONF = 'django_react_proj.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'students-fe/build')],
        '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 = 'django_react_proj.wsgi.application'


# Database
# https://docs.djangoproject.com/en/3.2/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': BASE_DIR / 'db.sqlite3',
    }
}


# Password validation
# https://docs.djangoproject.com/en/3.2/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]


# Internationalization
# https://docs.djangoproject.com/en/3.2/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True

DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
django_heroku.settings(locals())

CORS_ALLOW_ALL_ORIGINS = True
CORS_ALLOWED_ORIGINS = (
    'https://vmbf.herokuapp.com',
)

CSRF_TRUSTED_ORIGINS = [
    'vmbf.herokuapp.com',
]

STATIC_ROOT = os.path.join(BASE_DIR,'students-fe', 'build', 'static')
STATIC_URL = '/static/'

STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'

And here is how my folder looks like:

Folder structure

Feel free to ask me more code, I'm new to Django / React therefore I'm not sure what I should share here or not.

CodePudding user response:

So I found out my problem.

It was not related to PostgreSQL, but thank you @Chris to pointing that out, it's better development methodology for myself.

The problem was related to how I managed the Static Files in my settings.py

I didn't understood how STATIC_ROOT works, I understood the other way around. After reflecting on it here is what I did:

STATIC_ROOT = os.path.join(BASE_DIR,'staticfiles')

My files will be served from this staticfiles directories.

STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'students-fe','build','static'),
]

But it has to include my react app that is in students-fe/build/static folder.

On top of that, when deploying to Heroku, it is necessary to add the buildpacks, which I did but not in the correct order.

First I needed to add python (for Django):

heroku buildpacks:set heroku/python

and then saying that before that it needs to install nodejs (for React):

heroku buildpacks:add --index 1 heroku/nodejs

Like this Heroku will first do a npm run build before the python manage.py collecstatic that is needed to move the static files into the staticfiles folder.

Hope it can help others. Thanks to all for your answers.

  • Related