Django apache manages static files

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

Definitely django admin css doesn't work and my home files are generated by css.

An example of my settings, including those related to managing static settings.py files:

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/'

      

The 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 an error log that appears frequently:

[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 is a link to my problem ...

I've already read a lot of answers and tried to use them, but changes to settings.py/apache are not enough yet. I also followed django tutorials with no results. I'm a little lost now because all the answers I found were different and never used the same way of setting up static files with apache.

+3


source to share


2 answers


Your STATIC_URL is wrong - given your apache conf it should be '/ static /' (and it should be a url, not a filesystem path anyway).



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

+3


source


To alert the working directory, which version of mod_wsgi is enabled? It looks like you can use the old version.

For your problem with static file access issue:



Alias /static/admin/ "/usr/local/lib/python2.7/dist-packages/django/contrib/admin/static/"

      

You need a trailing slash in the target path if you are doing it in the URL path.

+1


source







All Articles