Django apache 管理静态文件

Django apache managing static files

自从我开始使用 Django 在 Python 中编程已经一个月了。最近我不得不在 Apache 服务器上部署我的第一个应用程序。一切顺利,除了静态文件的管理方式。

当然,django admin css 不工作,我自制的 css 文件也是如此。

我的设置示例,包括与静态文件管理相关的内容 settings.py :

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

当前的 apache httpd conf(编辑于 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

顺便说一句,我有那个经常产生的错误日志:

[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'.

不知道有没有link我的问题...

我之前已经阅读了很多答案,并尝试使用它们,但是 settings.py/apache 配置中的更改目前还不够。我也跟着 django 教程没有结果。现在我有点迷茫,因为我找到的所有答案都是不同的,而且从来没有使用相同的方法来使用 apache 设置静态文件。

你的STATIC_URL错了。给定您的 Apache 配置,它应该是 /static/(无论如何它应该是 URL,而不是文件系统路径)。

而且我认为您的 Apache 日志中的错误与您的问题无关。

对于工作目录警告,您使用的 mod_wsgi 版本是什么?看来您使用的可能是旧版本。

对于您的静态文件访问问题更改为使用:

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

如果要在 URL 路径上添加斜杠,则目标路径上需要一个尾部斜杠。