HTML templates isn't rendering with debug True. (heroku)

My HTML templates are not rendering with I have my debug turned off/Debug False It throws a “Server Error 500” But if I try rendering a HttpResponse it works.

My whole project works completely fine when I have my debug on True

Please reply if you can help me.

settings.py

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

ALLOWED_HOSTS = ['{{ProjectName}}.herokuapp.com', '127.0.0.1']


# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',

    # Custom Apps
    'authSystem',
    'posts',
    
]

MIDDLEWARE = [
    'whitenoise.middleware.WhiteNoiseMiddleware',

    'django.middleware.security.SecurityMiddleware',
    '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 = 'src.urls'

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',
            ],
        },
    },
]

WSGI_APPLICATION = 'src.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 = 'Asia/Kolkata'

USE_I18N = True

USE_L10N = True

USE_TZ = True


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

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

STATIC_URL = '/static/'

MEDIA_URL = '/images/'

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

MEDIA_ROOT = os.path.join(BASE_DIR, 'static/images')

I have my templates inside “templates” folders in the BASE_DIR.

if anyone need me to provide any more files please let me know.

How are you running your application? Through gunicorn, uwsgi, or something else?
(Regardless of the server, it would be helpful to see the complete traceback from the server for the error being thrown. Knowing which server is being used might help identify where to find that information if you don’t know where to look.)

I’m using gunicorn.

IDK what I did, but now it doesn’t show “Server Error 500” but it shows “Bad Request 400”

and here are the logs

2021-08-28T12:30:52.486810+00:00 heroku[web.1]: Restarting
2021-08-28T12:30:52.517386+00:00 heroku[web.1]: State changed from up to starting
2021-08-28T12:30:53.311722+00:00 heroku[web.1]: Stopping all processes with SIGTERM
2021-08-28T12:30:53.371079+00:00 app[web.1]: [2021-08-28 18:00:53 +0530] [7] [INFO] Worker exiting (pid: 7)
2021-08-28T12:30:53.383550+00:00 app[web.1]: [2021-08-28 18:00:53 +0530] [8] [INFO] Worker exiting (pid: 8)
2021-08-28T12:30:53.387276+00:00 app[web.1]: [2021-08-28 12:30:53 +0000] [4] [INFO] Handling signal: term
2021-08-28T12:30:53.391882+00:00 app[web.1]: [2021-08-28 12:30:53 +0000] [4] [WARNING] Worker with pid 7 was terminated due to signal 15
2021-08-28T12:30:53.400827+00:00 app[web.1]: [2021-08-28 12:30:53 +0000] [4] [WARNING] Worker with pid 8 was terminated due to signal 15
2021-08-28T12:30:53.487848+00:00 app[web.1]: [2021-08-28 12:30:53 +0000] [4] [INFO] Shutting down: Master
2021-08-28T12:30:53.574527+00:00 heroku[web.1]: Process exited with status 0
2021-08-28T12:30:56.112084+00:00 heroku[web.1]: Starting process with command `gunicorn src.wsgi`
2021-08-28T12:30:58.396631+00:00 app[web.1]: [2021-08-28 12:30:58 +0000] [4] [INFO] Starting gunicorn 20.1.0
2021-08-28T12:30:58.398058+00:00 app[web.1]: [2021-08-28 12:30:58 +0000] [4] [INFO] Listening at: http://0.0.0.0:9183 (4)
2021-08-28T12:30:58.398103+00:00 app[web.1]: [2021-08-28 12:30:58 +0000] [4] [INFO] Using worker: sync
2021-08-28T12:30:58.401255+00:00 app[web.1]: [2021-08-28 12:30:58 +0000] [7] [INFO] Booting worker with pid: 7
2021-08-28T12:30:58.476309+00:00 app[web.1]: [2021-08-28 12:30:58 +0000] [8] [INFO] Booting worker with pid: 8
2021-08-28T12:30:58.990153+00:00 heroku[web.1]: State changed from starting to up
2021-08-28T12:31:02.000000+00:00 app[api]: Build succeeded
2021-08-28T12:33:09.834306+00:00 app[web.1]: 10.1.43.26 - - [28/Aug/2021:18:03:09 +0530] "GET /auth/login/ HTTP/1.1" 500 145 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36"
2021-08-28T12:33:09.834524+00:00 heroku[router]: at=info method=GET path="/auth/login/" host=saudz.herokuapp.com request_id=dfd8aa6a-3f2c-4470-910b-f8dd2a9c6208 fwd="49.205.247.80" dyno=web.1 connect=0ms service=110ms status=500 bytes=403 protocol=https
2021-08-28T12:33:10.852792+00:00 app[web.1]: 10.1.43.26 - - [28/Aug/2021:18:03:10 +0530] "GET /favicon.ico HTTP/1.1" 404 179 "https://saudz.herokuapp.com/auth/login/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36"
2021-08-28T12:33:10.853067+00:00 heroku[router]: at=info method=GET path="/favicon.ico" host=saudz.herokuapp.com request_id=c1594f16-4684-45f9-ad62-10bd3dec8c35 fwd="49.205.247.80" dyno=web.1 connect=0ms service=2ms status=404 bytes=411 protocol=https
2021-08-28T12:33:13.898771+00:00 app[web.1]: 10.1.43.26 - - [28/Aug/2021:18:03:13 +0530] "GET / HTTP/1.1" 302 0 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36"
2021-08-28T12:33:13.898939+00:00 heroku[router]: at=info method=GET path="/" host=saudz.herokuapp.com request_id=c232823e-d1d2-4870-b852-456222226b03 fwd="49.205.247.80" dyno=web.1 connect=0ms service=14ms status=302 bytes=279 protocol=https
2021-08-28T12:33:14.177201+00:00 app[web.1]: 10.1.43.26 - - [28/Aug/2021:18:03:14 +0530] "GET /auth/login/ HTTP/1.1" 500 145 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36"
2021-08-28T12:33:14.177402+00:00 heroku[router]: at=info method=GET path="/auth/login/" host=saudz.herokuapp.com request_id=97af517a-972f-46b8-9251-8e13b1bf74e4 fwd="49.205.247.80" dyno=web.1 connect=0ms service=12ms status=500 bytes=403 protocol=https
2021-08-28T12:34:33.281537+00:00 app[web.1]: 10.1.2.164 - - [28/Aug/2021:18:04:33 +0530] "GET /auth/login/ HTTP/1.1" 500 145 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36"
2021-08-28T12:34:33.283523+00:00 heroku[router]: at=info method=GET path="/auth/login/" host=saudz.herokuapp.com request_id=ede5b943-538d-4893-9470-74e1b7c2fb11 fwd="49.205.247.80" dyno=web.1 connect=0ms service=58ms status=500 bytes=403 protocol=https
2021-08-28T12:34:34.150792+00:00 app[web.1]: 10.1.2.164 - - [28/Aug/2021:18:04:34 +0530] "GET /favicon.ico HTTP/1.1" 404 179 "https://saudz.herokuapp.com/auth/login/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36"
2021-08-28T12:34:34.152738+00:00 heroku[router]: at=info method=GET path="/favicon.ico" host=saudz.herokuapp.com request_id=012f8a18-3c73-42e9-bd71-2a7d27232f07 fwd="49.205.247.80" dyno=web.1 connect=0ms service=3ms status=404 bytes=411 protocol=https

Ahh, it looks like you’re also using heroku. (That would have been useful to know up front as well - these types of deployment issues depend a lot on the runtime environment.)

Hopefully one of the more heroku-knowledgeable people on this forum can help. (I don’t use heroku, and don’t really know anything about it.)

1 Like