简体   繁体   English

带有Apache2的Django管理页面

[英]Django Admin page with Apache2

I am running Ubuntu 14.04 on my notebook and I am using Django 1.6.4 (with virtualenv) together with Apache 2.4.7. 我在笔记本上运行Ubuntu 14.04,并且将Django 1.6.4(带有virtualenv)与Apache 2.4.7一起使用。

I have set up a django project that is located under my home directory in /home/nick/Workspace/Web/kleyboldt_django (kleyboldt is the name of the guy I am writing this project for). 我已经在/home/nick/Workspace/Web/kleyboldt_django我的主目录下设置了一个django项目(kleyboldt是我为此项目编写的人的名字)。 This folder contains my virtual env folder (django and south installed), my kleyboldt.wsgi and the actual project called kleyboldt_homepage . 该文件夹包含我的虚拟env文件夹(已安装django和south),我的kleyboldt.wsgi和名为kleyboldt_homepage的实际项目。 To make my site ready for production I wrote a config file to create a VirtualHost serving this site: 为了使我的站点可以投入生产,我编写了一个配置文件来创建为该站点提供服务的VirtualHost:

/etc/apache2/sites-available/mks.conf /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>

I edited /etc/hosts in order to redirect mks.com to my local maschine. 我编辑了/etc/hosts以便将mks.com重定向到我的本地机器。 The settings file looks like that: 设置文件如下所示:

/home/nick/Workspace/Web/kleyboldt_django/kleyboldt_homepage/kleyboldt_homepage/settings.py /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',
)
...

In kleyboldt_homepage/static/ are four folder: media (data uploaded by the user which you should not trust), static (folders css , fonts , img and js for Bootstrap), static-only (contains admin , css , fonts , img and js after ./manage.py collectstatic ) and templates . 在kleyboldt_homepage / static /中,有四个文件夹: media (用户不信任的用户上传的数据), static (Bootstrap的文件夹cssfontsimgjs ),仅静态(包含admincssfontsimg./manage.py collectstatic )和templates之后的js

Unfortunately my admin site is still without css and opening the css files in the source code leads to an error message. 不幸的是,我的管理站点仍然没有CSS,并且在源代码中打开CSS文件会导致错误消息。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM