css - 带有 Apache2 的 Django 管理页面

标签 css django apache django-admin apache-config

我在我的笔记本电脑上运行 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(文件夹 cssfontsimgjs 用于 Bootstrap),仅静态(包含 admincss , fonts, img and jsafter ./manage.py collectstatic) and 模板

不幸的是,我的管理站点仍然没有 css,打开源代码中的 css 文件会导致出现错误消息。

最佳答案

在 settings.py 中设置 STATIC_ROOTSTATIC_URL 后运行 python manage.py collectstatic

关于css - 带有 Apache2 的 Django 管理页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23432652/

相关文章:

PHP rename() 忽略权限?

html - @Html.EditorFor 中的属性 css

javascript - 在首次加载时运行快速 Javascript 基准测试

css - 如何在底部为 :0px? 的情况下将包装器居中

django - 上传大文件时如何避免空闲连接超时?

apache - 我如何将经过身份验证的用户从 apache kerberos 传递到 tomcat GET 请求

python - Apache mod_wsgi Django 设置 - 禁止您无权访问此服务器上的/mysite

html - 带有按钮的导航栏在单击时获得事件属性但在选择其他按钮时丢失它们

Django:使用 order_by 对数值进行排序

Python - 为实例覆盖 __getattribute__?