Help with staticfiles in deployment

56 views
Skip to first unread message

Alexander Joseph

unread,
Sep 30, 2017, 8:40:46 PM9/30/17
to Django users
Hello,

My site is deployed on an ubuntu/nginx/gunicorn droplet on digitalOcean. For some reason my static files are not updating after running collectstatic. I have access to older static files but cant make updates to static files and see them in production. It works as it should on my development computer. Its kind of like my staticfiles storage is some CDN that I wasnt aware of and now when I run collectstatic its not putting the files in the CDN anymore

Below is my base settings configuration

# settings/base.py

import environ

ROOT_DIR = environ.Path(__file__) - 3  # (business_management/config/settings/base.py - 3 = business_management/)
APPS_DIR = ROOT_DIR.path('business_management')

# Load operating system environment variables and then prepare to use them
env = environ.Env()

# .env file, should load only in development environment
READ_DOT_ENV_FILE = env.bool('DJANGO_READ_DOT_ENV_FILE', default=False)

if READ_DOT_ENV_FILE:
    # Operating System Environment variables have precedence over variables defined in the .env file,
    # that is to say variables from the .env files will only be used if not defined
    # as environment variables.
    env_file = str(ROOT_DIR.path('.env'))
    print('Loading : {}'.format(env_file))
    env.read_env(env_file)
    print('The .env file has been loaded. See base.py for more information')


# DEBUG
# ------------------------------------------------------------------------------
DEBUG = env.bool('DJANGO_DEBUG', False)


# Application definition
# ------------------------------------------------------------------------------

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    
    'django.contrib.sites',
    'allauth',
    'allauth.account',
    'allauth.socialaccount',
    'allauth_office365',
    
    'bootstrap4',
    
    'business_management.accounts',
    'business_management.engineering',
]

#SOCIALACCOUNT_ADAPTER = 'allauth_office365.adapter.SocialAccountAdapter'
SOCIALACCOUNT_EMAIL_VERIFICATION = False

SOCIALACCOUNT_PROVIDERS = {
    'office365': {
      'SCOPE': ['User.read',],
      'USERNAME_FIELD': 'mail'
    }
}

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 = 'config.urls'


TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [
            str(APPS_DIR.path('templates')),
        ],
        'OPTIONS': {
            'debug': DEBUG,
            'loaders': [
                'django.template.loaders.filesystem.Loader',
                'django.template.loaders.app_directories.Loader',
            ],
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.template.context_processors.i18n',
                'django.template.context_processors.media',
                'django.template.context_processors.static',
                'django.template.context_processors.tz',
                'django.contrib.messages.context_processors.messages',
                # Your stuff: custom template context processors go here
            ],
        },
    },
]


WSGI_APPLICATION = 'config.wsgi.application'


# Password validation
# ------------------------------------------------------------------------------

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

AUTHENTICATION_BACKENDS = (
    # Needed to login by username in Django admin, regardless of `allauth`
    'django.contrib.auth.backends.ModelBackend',
    # `allauth` specific authentication methods, such as login by e-mail
    'allauth.account.auth_backends.AuthenticationBackend',
)


# Internationalization
# ------------------------------------------------------------------------------
LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'America/Denver'

USE_I18N = True

USE_L10N = True

USE_TZ = True


STATICFILES_FINDERS = [
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
]


# AllAuth Settings
# ------------------------------------------------------------------------------

AUTH_USER_MODEL = 'accounts.User'

SITE_ID = 1

LOGIN_REDIRECT_URL = 'dashboard'
ACCOUNT_EMAIL_REQUIRED = True # email required to signup
ACCOUNT_EMAIL_VERIFICATION = False # email verification manditory for account verification
ACCOUNT_AUTHENTICATION_METHOD = "username_email" # username or email
#ACCOUNT_LOGIN_ON_EMAIL_CONFIRMATION = True
#VERIFIED_EMAIL = True
#ACCOUNT_FORMS = {'login': 'accounts.forms.MyLoginForm', 'sign_up': 'accounts.forms.MySignupForm'}
ACCOUNT_LOGIN_ATTEMPTS_LIMIT = 5
ACCOUNT_LOGIN_ATTEMPTS_TIMEOUT = 300
#ACCOUNT_SIGNUP_FORM_CLASS = 'business_management.accounts.forms.UserCreateForm'
ACCOUNT_USERNAME_MIN_LENGTH = 4
ACCOUNT_LOGOUT_REDIRECT_URL = 'account_login'
#ACCOUNT_SESSION_REMEMBER = None # Controls the life time of the session. Set to None to ask the user 'Remember me?', False to not remember, and True to always remember.


and below is my production settings (sensitive data omitted)...

from .base import *

import json


DEBUG = False

WHITENOISE_MIDDLEWARE = ['whitenoise.middleware.WhiteNoiseMiddleware', ]
MIDDLEWARE = WHITENOISE_MIDDLEWARE + MIDDLEWARE

INSTALLED_APPS += ['gunicorn', ]

# Static File Configuration
# ------------------------------------------------------------------------------
STATIC_ROOT = str(ROOT_DIR('staticfiles'))

STATIC_URL = "/staticfiles/"

STATICFILES_DIRS = [
    str(APPS_DIR.path('static')),
]

When i run collectstatic new static files are going to the staticfolder where I expect them to go also. Thanks for your help!

Antonis Christofides

unread,
Oct 1, 2017, 10:56:13 AM10/1/17
to django...@googlegroups.com

Hi,

1) What is environ? Could you point to its documentation page?

2) Could you show your nginx configuration?

Antonis Christofides
http://djangodeployment.com
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/3e9f167b-6c09-4dec-85b2-a1617a37646e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Alexander Joseph

unread,
Oct 1, 2017, 11:37:04 PM10/1/17
to Django users
environ allows you to use environment variables in your settings files, heres the docs

and my nginx config look like this... thanks

server {
    listen 80;
    server_name [domain name];
    ssl_dhparam /etc/ssl/certs/dhparam.pem;

    location = /favicon.ico { access_log off; log_not_found off; }
    location /staticfiles/ {
        root /home/business_management;
    }

    location / {
        include proxy_params;
        proxy_pass http://unix:/home/business_management/business_management.sock;
    }
}


Alexander Joseph

unread,
Oct 2, 2017, 12:05:51 PM10/2/17
to Django users
It was my nginx configuration. I hadnt noticed the staticfiles directory was pointing to the wrong place. Thanks for the help!
Reply all
Reply to author
Forward
0 new messages