我在我的笔记本电脑上运行 Ubuntu 14.04,我正在使用 Django 1.6.4(带有 virtualenv)和 Apache 2.4.7。
我已经在 /home/nick/Workspace/Web/kleyboldt_django
中的主目录下设置了一个 django 元素(kleyboldt 是我为其编写此元素的人的名字) .此文件夹包含我的虚拟 env
文件夹(安装了 django 和 south)、我的 kleyboldt.wsgi
和名为 kleyboldt_homepage
的实际元素。为了让我的站点准备好投入生产,我编写了一个配置文件来创建一个为该站点提供服务的 VirtualHost:
/etc/apache2/sites-available/mks.conf
WSGIDaemonProcess mks.com python-path=/home/nick/Workspace/Web/kleyboldt_django/kleyboldt_homepage:/home/nick/Workspace/Web/kleyboldt_django/env/lib/python2.7/site-packages
WSGIProcessGroup mks.com
<VirtualHost *:80>
Alias /robots.txt /home/nick/Workspace/Web/kleyboldt_django/kleyboldt_homepage/static/robots.txt
Alias /favicon.ico /home/nick/Workspace/Web/kleyboldt_django/kleyboldt_homepage/static/favicon.ico
AliasMatch ^/([^/]*\.css) /home/nick/Workspace/Web/kleyboldt_django/kleyboldt_homepage/static-only/css/$1
Alias /media/ /home/nick/Workspace/Web/kleyboldt_django/kleyboldt_homepage/static/media/
Alias /static/ /home/nick/Workspace/Web/kleyboldt_django/kleyboldt_homepage/static/static/
Alias /static/admin/ /home/nick/Workspace/Web/kleyboldt_django/env/lib/python2.7/site-packages/django/contrib/admin/static/admin/
<Directory /home/nick/Workspace/Web/kleyboldt_django/kleyboldt_homepage/static/static>
Require all granted
</Directory>
<Directory /home/nick/Workspace/Web/kleyboldt_django/kleyboldt_homepage/static/media>
Require all granted
</Directory>
WSGIScriptAlias / /home/nick/Workspace/Web/kleyboldt_django/kleyboldt.wsgi
ServerName mks.com
Alias /static /home/nick/Workspace/Web/kleyboldt_django/kleyboldt_homepage/static
<Directory /home/nick/Workspace/Web/kleyboldt_django/>
Require all granted
Order allow,deny
Allow from all
</Directory>
<Directory /home/nick/Workspace/Web/kleyboldt_django/env/lib/python2.7/site-packages/>
Require all granted
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
我编辑了 /etc/hosts
以将 mks.com
重定向到我的本地机器。设置文件如下所示:
/home/nick/Workspace/Web/kleyboldt_django/kleyboldt_homepage/kleyboldt_homepage/settings.py
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
...
MEDIA_ROOT = os.path.join(BASE_DIR, 'static', 'media')
MEDIA_URL = '/media/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static', 'static-only')
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static', 'static'),
)
TEMPLATE_DIRS = (
os.path.join(BASE_DIR, 'static', 'templates'),
)
ADMIN_MEDIA_PREFIX = '/static/admin/'
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'south',
'homepage',
)
MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
)
...
在 kleyboldt_homepage/static/中有四个文件夹:media
(您不应信任的用户上传的数据),static
(文件夹 css
、fonts
、img
和 js
用于 Bootstrap),仅静态(包含 admin
、css
, fonts
, img
and js
after ./manage.py collectstatic
) and 模板
。
不幸的是,我的管理站点仍然没有 css,打开源代码中的 css 文件会导致出现错误消息。
最佳答案
在 settings.py 中设置 STATIC_ROOT
和 STATIC_URL
后运行 python manage.py collectstatic
关于css - 带有 Apache2 的 Django 管理页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23432652/