Search code examples
pythondjangoapachestatic-files

Django apache managing static files


It's been a month since I started programming in Python with Django. Recently I had to deploy my first app on an Apache server. Everything went well, except how static files are managed.

Definitely, django admin css is not working, and so does my home made css files.

A sample of my settings, including what is related to static files management settings.py :

EDIT settings.py (10/06/2015):

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

DEBUG = True

ALLOWED_HOSTS = ['foo.moo']

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'formulaire',
)

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

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

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'project_static'),
)

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


DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'test_front_end',
        'USER': '******',
        'PASSWORD': '*****',
        'HOST': 'censored',
        'PORT': '5432',
    }
}

LANGUAGE_CODE = 'fr-fr'

TIME_ZONE = 'Europe/Paris'

USE_I18N = True

USE_L10N = True

USE_TZ = True


STATIC_URL = '/static/'

Current apache httpd conf (edited 10/06/2015) :

 <VirtualHost *:80>
        ServerAdmin webmaster@localhost
        DocumentRoot /var/www
        ServerName www.foo.moo


        ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
        <Directory "/usr/lib/cgi-bin">
                AllowOverride None
                Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
                Order allow,deny
                Allow from all
        </Directory>

          WSGIDaemonProcess daemonschift python-path=/var/www/schift/
WSGIProcessGroup daemonschift
Alias /static/admin/ "/usr/local/lib/python2.7/dist-packages/django/contrib/admin/static/"
Alias /static/ /var/www/schift/static/
<Directory /var/www/schift/>
    <Files wsgi.py>
        Order allow,deny
        Allow from all
    </Files>
</Directory>
<Directory /var/www/schift/static/>
   Order allow,deny
   Allow from all
</Directory>
<Directory "/usr/local/lib/python2.7/dist-packages/django/contrib/admin/static/">
   Order allow,deny
   Options Indexes
   Allow from all
   IndexOptions FancyIndexing

ErrorLog ${APACHE_LOG_DIR}/error.log
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/access.log combined

By the way, I have that error log that oftenly spawns :

[Tue Jun 02 11:59:28 2015] [alert] (2)No such file or directory: mod_wsgi (pid=19643): Unable to change working directory to '/usr/local/apache2/htdocs'.

I don't know if there's a link with my problem...

I've read a lot of answers before, and tried to use them, but changes in settings.py / apache config were not enough so far. I also followed django tutorials with no results. Right now I'm a bit lost, because all the answers I found were different and were never using the same ways to set up static files with apache.


Solution

  • Your STATIC_URL is wrong. Given your Apache configuration, it should be /static/ (and it should be a URL anyway, not a filesystem path).

    And I don't think the error in your Apache log has anything to do with your problem.