gpt4 book ai didi

python - Django apache 管理静态文件

转载 作者:太空宇宙 更新时间:2023-11-03 15:07:56 25 4
gpt4 key购买 nike

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

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

我的设置示例,包括与静态文件管理相关的内容设置.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'.

我不知道是否与我的问题有关...

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

最佳答案

您的 STATIC_URL 是错误的。鉴于您的 Apache 配置,它应该是 /static/(无论如何它应该是一个 URL,而不是文件系统路径)。

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

关于python - Django apache 管理静态文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30593778/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com