0

I was able to get my static files (CSS) to load in my local development environment, however when I push the changes to my development server I am unable to load the CSS. My local environment is Mac OS 10.9.2 and my development server is running Ubuntu 12.04.4 x64.

settings.py

import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
STATICFILES_DIRS = (
  os.path.join(BASE_DIR, 'static/'),
)

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

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '#7h&rny3^hz&q6w-8$6k&+msh554$pz*tx@$lj(+dgctvuj2j%'

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

TEMPLATE_DEBUG = True

ALLOWED_HOSTS = []

AUTHENTICATION_BACKENDS = (

    'django.contrib.auth.backends.ModelBackend',
    'allauth.account.auth_backends.AuthenticationBackend',

)

TEMPLATE_CONTEXT_PROCESSORS = (

    'django.core.context_processors.request',
    'allauth.account.context_processors.account',
    'allauth.socialaccount.context_processors.socialaccount',
    'django.contrib.auth.context_processors.auth',

)

# Application definition

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'pcatapp',
    'south',
    'django.contrib.sites',
    'allauth',
    'allauth.account',
    'allauth.socialaccount',
    'tastypie',
)

MIDDLEWARE_CLASSES = (
    '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',
    #'django.core.context_processors.csrf',
)

ROOT_URLCONF = 'pcat.urls'

WSGI_APPLICATION = 'pcat.wsgi.application'


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

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql', 
        'NAME': 'db',
        'USER': 'root',
        'PASSWORD': 'password',
        'HOST': 'localhost',   # Or an IP Address that your DB is hosted on
        'PORT': '3306',
    }
}

# Internationalization
# https://docs.djangoproject.com/en/1.6/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True

SITE_ID = 1

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.6/howto/static-files/

STATIC_URL = '/static/' 

An excerpt from the page source:

<link rel="stylesheet" href="/static/myapp/style.css">

My static folder is located at myapp/static/myapp/style.css. All help is appreciated, thanks.

6
  • I just tried setting my STATIC_ROOT = /static/ and running python manage.py collectstatic which did not seem to change anything. Commented Jun 9, 2014 at 22:36
  • Have you setup your server to serve the static files? also STATIC_ROOT should point to a full path, /static/ means you are serving from your root folder static. Commented Jun 9, 2014 at 22:38
  • 5
    You need to setup your server to serve the static files. It's not handled in production by Django. See docs.djangoproject.com/en/dev/howto/static-files/#deployment Commented Jun 9, 2014 at 22:42
  • Here's an awesome tutorial that helped deploy projects. Also have a look at this one. Commented Jun 10, 2014 at 0:07
  • I like this post about serving static: hasnath.net/blog/… Commented Jun 10, 2014 at 4:33

2 Answers 2

3

I had the same problem and dj-static provided a simple solution

$ pip install dj-static

In your wsgi.py file:

from django.core.wsgi import get_wsgi_application
from dj_static import Cling

application = Cling(get_wsgi_application())

Good Luck

Sign up to request clarification or add additional context in comments.

Comments

1

When using djangos development server, I have found that this problem, and its solution normally lies in the URLS file (when DEBUG=True), something like this will allow you to use django to serve your static files:

from django.conf import settings
from django.conf.urls.static import static


urlpatterns = patterns(
    '',
    ... url includes etc.
) + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

This will mean that any urls serving from {{ STATIC_URL }} will be handled by django, here are the django docs on this https://docs.djangoproject.com/en/dev/howto/static-files/

EDIT (Added solution for running under a webserver not django development server):

If you are running your project behind a webserver, and not using djangos runserver, then its probably best you dont use djangos static file finder, and instead use its static file collector and the webserver to serve static files, to do this, try this in your settings.py:

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

then run ./manage.py collectstatic and ensure your webserver redirects requests from /static/ to the full path of your static files directory, e.g in NGINX you might do..

location /static/ {
    alias   /var/www/myproject/static/;
}

And apache:

Alias /static/ /var/www/myproject/static/

Hope this helps...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.