Search code examples
pythondjangoherokudjango-templateshttp-status-code-500

Django returning 500 Internal Server Error instead of index.html when Debug = False


I have a problem in my django web application in which django returns a 500 error when trying to get the index.html file. This only happens when Debug = False and it only happens with this one template. All the other templates render normally without errors.

I have already tried the whitenoise settings, favicon.ico errors, checked all routes and everything seems to be fine, I really can't find the error. The weird thing is that it is only happening in index.html.

If someone can help I will really appreciate it, thanks in advance.

settings.py

DEBUG = False
ALLOWED_HOSTS = ['vilytic.herokuapp.com', '127.0.0.1']

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

REST_FRAMEWORK = {
    'DEFAULT_THROTTLE_RATES': {
        'video_search': '8/day',
        'video_id': '8/day'
    }
}

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'whitenoise.middleware.WhiteNoiseMiddleware',
    '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',
]

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'templates')],
        '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',
            ],
        },
    },
]

STATIC_URL = '/static/'

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

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

STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'

Error that appears in terminal

"GET / HTTP/1.1" 500 145

urls.py

from django.contrib import admin
from django.urls import path, include
from . import views
from django.views.generic.base import RedirectView
from django.conf import settings
from django.contrib.staticfiles.storage import staticfiles_storage

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', views.index, name="index"),
    path('accounts/', include('accounts.urls'), name="accounts"),
    path('comparer/', include('comparer.urls'), name="comparer"),
    path('contact/', views.contact, name="contact"),
    path(
        "favicon.ico",
        RedirectView.as_view(url=staticfiles_storage.url("favicon.ico")),
    ),
]

views.py

from django.shortcuts import render
from django.core.mail import EmailMessage
from django.template.loader import render_to_string
from django.conf import settings


def index(request):
    return render(request, 'index.html')

Solution

  • It turns out that a png file was causing the problem. I don't know why png files sometimes get saved with capital letters, like PNG. Apparently that was causing a problem with whitenoise. It took me a long time to figure it out, hope it helps.