0

Moved my project to my production server on namescheap. Noticing however that while the pages are loading the static files (css,js and images) do not seem to be loading.

I have tried to read the django documentation to understand if there are any settings that I need to change when moving from development to production and tried to read similar questions on stackoverflow but still have the same issue.

This is what I have in my settings

...
DEBUG = False
# DEBUG = os.environ.get('DJANGO_DEBUG', '') != 'False'

ALLOWED_HOSTS = ["thegradientboost.com", "localhost", "127.0.0.1","http://thegradientboost.com/gradientboost"]


# Application definition

INSTALLED_APPS = [
    'django.contrib.auth',
    'django.contrib.admin',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    # 'whitenoise.runserver_nostatic',
    'django.contrib.staticfiles',
    'django.contrib.humanize',

    'crispy_forms',

    'classroom',
]

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

ROOT_URLCONF = 'django_school.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 = 'django_school.wsgi.application'

...
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'allstaticfiles')


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


CRISPY_TEMPLATE_PACK = 'bootstrap4'

#deployment 
SECURE_HSTS_INCLUDE_SUBDOMAINS = True
SECURE_SSL_REDIRECT = True
SECURE_HSTS_SECONDS = 60
SESSION_COOKIE_SECURE = True
CSRF_COOKIE_SECURE = True
SECURE_HSTS_PRELOAD = True
SECURE_REFERRER_POLICY = "same-origin"

wsgi:

import os

from django.core.wsgi import get_wsgi_application

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "django_school.settings")

application = get_wsgi_application()

the structure of the files in my project

gradboost
-->__pycache__
-->allstaticfiles
     admin
     css
     second
     third
     fourth
-->classroom
       __init__.py
       apps.py
       decorators.py
       forms.py
       models.py
       urls.py
------>templates
------>templatetags
-->django_school
       __pycache__
       __init__.py
       settings.py
       urls.py
       wsgi.py
-->public
-->static
     css
     second
     third
     fourth
-->templates
-->tmp
   manage.py
   passenger_wsgi.py
public_html

and how I load static in my html pages

...
{ % load static %}
...
<link rel="stylesheet" href="{% static 'third/css/bootstrap.css' %}">
Emm
  • 2,367
  • 3
  • 24
  • 50
  • Django does *not* serve static files if `DEBUG` is set to `False`, because Django's handling of static files is inefficient, and likely unsafe. You should configure `nginx`/`apache`/... to serve static files. – Willem Van Onsem Jan 22 '20 at 10:48
  • See the [documentation on *deploying static files*](https://docs.djangoproject.com/en/3.0/howto/static-files/deployment/) for more info. – Willem Van Onsem Jan 22 '20 at 10:49
  • Does this answer your question? [Why does DEBUG=False setting make my django Static Files Access fail?](https://stackoverflow.com/questions/5836674/why-does-debug-false-setting-make-my-django-static-files-access-fail) – dirkgroten Jan 22 '20 at 13:14

1 Answers1

1

With Debug True Django manages to load all static files at a low level (and processes too). With Debug False Django can't upload the files, it's not made for that. In order to work with static files correctly, I recommend you to use Django in production with Gunicorn + Nginx.

It also leaves your staticfiles well sorted, use it:

python manage.py collectstatic

My Github project can help you:

Github/Nginx

Nouvellie
  • 533
  • 4
  • 9