1

Running an app in Django. When Debug=True it all works fine. When Debug=False most of the app works fine except from a few static files, from what I was able to understand js files.

I run already python manage.py collectstatic and the css of the templates work, so the processs seems to have been succesful. Still though, a few JS files don't get loaded and this is messing up my admin panel.

This is the terminal error for the production environment (debug=False)

[21/Feb/2021 17:38:00] "GET /admin/ HTTP/1.1" 200 7803
[21/Feb/2021 17:38:00] "GET /static/admin/css/dashboard.css HTTP/1.1" 200 380
[21/Feb/2021 17:38:00] "GET /static/admin/img/icon-changelink.svg HTTP/1.1" 200 380
[21/Feb/2021 17:38:09] "GET /admin/amonthatatime/post/ HTTP/1.1" 200 9867
[21/Feb/2021 17:38:09] "GET /admin/jsi18n/ HTTP/1.1" 200 3187
[21/Feb/2021 17:38:11] "GET /admin/amonthatatime/post/1/change/ HTTP/1.1" 200 35171
[21/Feb/2021 17:38:11] "GET /admin/jsi18n/ HTTP/1.1" 200 3187
[21/Feb/2021 17:38:11] "GET /summernote/editor/id_medley/ HTTP/1.1" 200 7148
[21/Feb/2021 17:38:11] "GET /summernote/editor/id_whatsapp/ HTTP/1.1" 200 7154
[21/Feb/2021 17:38:11] "GET /static/summernote/lang/summernote-en-US.min.js?_=1613929091381 HTTP/1.1" 200 27
[21/Feb/2021 17:38:11] "GET /static/summernote/lang/summernote-en-US.min.js?_=1613929091408 HTTP/1.1" 200 27

This is my settings.py

from pathlib import Path
from django.conf import settings
from django.conf.urls.static import static
import os
import psycopg2
import dj_database_url
import cloudinary

# 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.1/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = os.environ.get('DJANGO_SECRET_KEY')


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

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


# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'amonthatatime.apps.AmonthatatimeConfig',
    'django_summernote',
    'cloudinary'
]

MIDDLEWARE = [
    '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 = 'giaggi.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        '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 = 'giaggi.wsgi.application'


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

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


# Password validation
# https://docs.djangoproject.com/en/3.1/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.1/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.1/howto/static-files/
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATIC_URL = '/static/'


MEDIA_URL = '/amonthatatime/static/amonthatatime/images/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'amonthatatime/static/amonthatatime/images/')
X_FRAME_OPTIONS = 'SAMEORIGIN'
#SECURE_SSL_REDIRECT=False
#SESSION_COOKIE_SECURE=False
#CSRF_COOKIE_SECURE=False



# Configure Django App for Heroku.
#import django_on_heroku
#django_on_heroku.settings(locals())

Danoram
  • 8,132
  • 12
  • 51
  • 71
giaggi
  • 542
  • 5
  • 16
  • 1
    When you turn DEBUG to False Django stops serving static files. You'll need a webserver like Nginx or Apache to serve them instead. – Danoram Feb 21 '21 at 18:22
  • Yeah, I am using gunicorn and running gunicorn giaggi.wsgi but still not working. Also, most of the static files are working (css). It's those few js that don't. I am losing my mind on it. – giaggi Feb 21 '21 at 19:07
  • 1
    Ok i see now. Could you include the `{% static 'url' %}` lines of these js files that are not loading? And also could you specify which js files it is that are not loading? The terminal output shows all the GET requests succeeded. Sorry for being pedantic – Danoram Feb 21 '21 at 19:20
  • I answered a very similar question here, I hope it can help https://stackoverflow.com/questions/65898805/django-app-getting-server-error-500-on-local-host-when-debug-false/65924431 – Dos Feb 21 '21 at 21:08
  • Thanks all! I have a good news and a bad news. The good news is that the problem went away. The bad news is that I don't know how i fixed it. I literally started from scratch, began a new django projects and copied models,views, admins et cetera one at a time and the problem disappeared. – giaggi Feb 22 '21 at 14:33

1 Answers1

0

I have also faced the same issue and run collectstatic as well. In my case, it improved not fully, but also I have found another ajax script and bootstrap bothered my CSS. (Just FYI, it was

https://cdnjs.cloudflare.com/ajax/libs/sweetalert/2.1.2/sweetalert.min.js)

  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/late-answers/30114342) – Fonic Oct 19 '21 at 08:29
  • It is not a link, script, 'cos when I added – Naoko Okada Oct 23 '21 at 11:38