user908759
user908759

Reputation: 1355

Django Production static files not loading

I have moved my Django 1.8 project to my production server. It is functional, but it is not loading my static files (css, js, or images) like my local server is. I have two repositories that have to same code base: my local machine and my production server. On my local machine my static files are found and displaying correctly. On the server they are not.

Here is a directory tree for production:

tmws
 - tmws
   - static
     - tmws
       - css
         screens.css
       - images
         xxxxxx.png
       main_style.css

Here is my settings.py for production

import os
from django.conf.global_settings import TEMPLATE_CONTEXT_PROCESSORS

MASTER_BASE_DIR = os.path.dirname(__file__)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.8/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'something'

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

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = (
    'polls',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'project',
    'django_tables2'
)

MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'django.middleware.security.SecurityMiddleware',
)

ROOT_URLCONF = 'tmws.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [
            os.path.join(MASTER_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',
                'django.core.context_processors.request'
            ],
        },
    },
]

WSGI_APPLICATION = 'tmws.wsgi.application'


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

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}


# Internationalization
# https://docs.djangoproject.com/en/1.8/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/1.8/howto/static-files/

STATIC_URL = '/static/'

STATICFILES_DIRS = (
    os.path.join(MASTER_BASE_DIR, "static"),
    #'/var/www/static/',
)

Here is my base.html for production

{% load staticfiles %}
<html>
<head>
    <meta charset="UTF-8">
    <title>Tyler Millwork and Supply</title>
    <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
    <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap-theme.min.css">
    <link rel="stylesheet" type="text/css" href="{% static 'tmws/main_style.css' %}" />
    <link rel="stylesheet" type="text/css" href="{% static 'tmws/css/screen.css' %}" />
    <link rel="stylesheet" type="text/css" href="{{ STATIC_URL }}django_tables2/themes/paleblue/css/screen.css" />
</head>
<body>
....

Here is my apache2.conf file for production

...
WSGIPythonPath /var/www/tmws
...

Here is my site.conf file for production

...
        ServerAdmin [email protected]
        ServerName tmws.hardinresources.com
        DocumentRoot /var/www/tmws
        WSGIScriptAlias / /var/www/tmws/tmws/wsgi.py

        <Directory /var/www/tmws/tmws>
               <Files wsgi.py>
                       Require all granted
               </Files>
        </Directory>
...

Here is my production site for production

my site

Here are the error I am receiving on my production server

http://tmws.hardinresources.com/static/tmws/main_style.css Failed to load resource: the server responded with a status of 404 (NOT FOUND)
http://tmws.hardinresources.com/static/tmws/css/screen.css Failed to load resource: the server responded with a status of 404 (NOT FOUND)
http://tmws.hardinresources.com/django_tables2/themes/paleblue/css/screen.css Failed to load resource: the server responded with a status of 404 (NOT FOUND)
http://tmws.hardinresources.com/static/tmws/images/TMWS_Logo.png Failed to load resource: the server responded with a status of 404 (NOT FOUND)
http://tmws.hardinresources.com/favicon.ico Failed to load resource: the server responded with a status of 404 (NOT FOUND)
http://tmws.hardinresources.com/static/tmws/css/screen.css Failed to load resource: the server responded with a status of 404 (NOT FOUND)
http://tmws.hardinresources.com/django_tables2/themes/paleblue/css/screen.css Failed to load resource: the server responded with a status of 404 (NOT FOUND)
http://tmws.hardinresources.com/static/tmws/main_style.css Failed to load resource: the server responded with a status of 404 (NOT FOUND)

Here are the runserver messages I am receiving on my local server

[23/May/2016 16:53:53] "GET / HTTP/1.1" 200 4061
[23/May/2016 16:53:53] "GET /django_tables2/themes/paleblue/css/screen.css HTTP/
1.1" 404 2354
[23/May/2016 16:53:56] "GET /static/tmws/main_style.css HTTP/1.1" 200 445
[23/May/2016 16:53:56] "GET /static/tmws/css/screen.css HTTP/1.1" 200 2774
[23/May/2016 16:53:56] "GET /static/tmws/img/header-bg.png HTTP/1.1" 404 1679
[23/May/2016 16:53:56] "GET /static/tmws/img/arrow-inactive-up.png HTTP/1.1" 404
 1703
[23/May/2016 16:53:56] "GET /static/tmws/img/pagination-bg.gif HTTP/1.1" 404 169
1
[23/May/2016 16:53:56] "GET /static/tmws/images/Seamless-Wood-1.jpg HTTP/1.1" 20
0 21678
[23/May/2016 16:53:56] "GET /static/tmws/images/TMWS_Logo.png HTTP/1.1" 200 1610
51
[23/May/2016 16:53:56] "GET /favicon.ico HTTP/1.1" 404 2252
[23/May/2016 16:54:03] "GET /django_tables2/themes/paleblue/css/screen.css HTTP/
1.1" 404 2354

How do I get my production server to load static files?

Thank you for any and all help!

Upvotes: 0

Views: 980

Answers (1)

Cody Parker
Cody Parker

Reputation: 1121

Try adding an alias to your static directory in the Apache config

Alias /static/ /var/www/tmws/twms/static/

And a Directory entry:

<Directory /var/www/tmws/twms/static>
Require all granted
</Directory>

...assuming your static directory is /var/www/tmws/twms/static/, that is.

And I would change your SECRET_KEY in the Django settings file now, since you've pasted it in your question above.

For reference: https://docs.djangoproject.com/en/1.9/howto/deployment/wsgi/modwsgi/#serving-files

Upvotes: 2

Related Questions