编程语言
首页 > 编程语言> > python-Django Apache管理静态文件

python-Django Apache管理静态文件

作者:互联网

自从我开始使用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(于2015年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 config中的更改还不够.我也遵循django教程,但没有结果.现在我有点迷失了,因为我发现的所有答案都是不同的,并且从未使用相同的方法通过apache设置静态文件.

解决方法:

您的STATIC_URL是错误的-给定您的apache conf,它应该是’/ static /'(无论如何它应该是url,而不是文件系统路径).

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

标签:apache,static-files,python,django
来源: https://codeday.me/bug/20191028/1951073.html