Joao Lucas
Joao Lucas

Reputation: 95

CSS disappears when 'DEBUG' Django is turned False

I'm working with Django and I'm using a AdminLTE framework for the estilização, but, when I turn the DEBUG to False, the page shows with pure html.

DEBUG = True: enter image description here

DEBUG = False:enter image description here

settings.py:

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

    'saga.core',
    'saga.subjects',
    'saga.accounts',
    'saga.tasks',
    'saga.study_time',
    'django_adminlte',
    'django_adminlte_theme',
]

django_adminlte and django_adminlte_theme are the apps for the style framework.

Upvotes: 0

Views: 1833

Answers (2)

Victor Mwendwa
Victor Mwendwa

Reputation: 1

When Debug = False , Django will not recognise the static files such as css and images. To make django aware you can follow the following steps :

  1. Install a python library "whitenoise" pip install whitenoise
  2. In your settings.py , find the middleware and add the file at the end. The file will look like this MIDDLEWARE = ['', 'whitenoise.middleware.WhiteNoiseMiddleware',]
  3. On the same settings.py add STATIC_ROOT = BASE_DIR / 'production_files. This will be used by django to create a folder to store all its static files
  4. You're almost done , now run the following command to generate the folder we stated above py manage.py collectstatic
  5. Re start your server and you should be good to go

For more information browse Here

Upvotes: 0

Stuart Dines
Stuart Dines

Reputation: 758

When DEBUG = True Django will serve static and media files. When DEBUG = False it will not. Therefore all of the js and css files will return a 404 error unless served. For a better of understanding of what is going on i would recommend reading https://docs.djangoproject.com/en/1.11/howto/static-files/ These static files may also be present in installed apps and not just within the project itself.

For local testing you can add the following to your urls.py urlpatterns:

urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

static can be imported from from django.conf.urls.static import static

Further information can be found at https://docs.djangoproject.com/en/1.11/howto/static-files/#serving-files-uploaded-by-a-user-during-development

Further information regarding how to deploy static files in production can be found at https://docs.djangoproject.com/en/1.11/howto/static-files/deployment/

Upvotes: 1

Related Questions