簡體   English   中英

Django apache管理靜態文件

[英]Django apache managing static files

自從我開始使用Django進行Python編程以來已經過去了一個月。 最近我不得不在Apache服務器上部署我的第一個應用程序。 一切順利,除了如何管理靜態文件。

當然,django admin css不能正常工作,我家的css文件也是如此。

我的設置示例,包括與靜態文件管理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/'

目前的apache httpd conf(2015年10月10日編輯):

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

我不知道我的問題是否有鏈接...

我之前已經閱讀了很多答案,並嘗試使用它們,但到目前為止,settings.py / apache配置中的更改還不夠。 我也跟着django教程沒有結果。 現在我有點迷失,因為我找到的所有答案都不同,並且從未使用相同的方法來設置帶有apache的靜態文件。

你的STATIC_URL是錯誤的 - 給你的apache conf,它應該是'/ static /'(無論如何它應該是一個url,而不是文件系統路徑)。

我認為您的apache日志中的錯誤與您的問題無關。

對於工作目錄警告,你在使用什么mod_wsgi版本? 看起來您可能正在使用舊版本。

對於您的靜態文件訪問問題更改為使用:

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

如果要在URL路徑上創建一個斜杠,則需要在目標路徑上使用尾部斜杠。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM