Apache没有为django提供管理静态文件

时间:2015-07-20 06:43:50

标签: python django apache mod-wsgi

在提出我的问题之前,我了解有关同一问题的评价很高的问题:fabs

但是,我已尝试过相同的解决方案,如下所示 这是我的apache conf文件:

WSGIScriptAlias / /home/ubuntu/sportsgullyrest/SportsGullyRest/wsgi.py
WSGIPythonPath /home/ubuntu/sportsgullyrest/venv/bin/python2.7

Alias /static/admin /usr/local/lib/python2.7/dist-packages/django/contrib/admin/static/admin
Alias /static/ /home/ubuntu/sportsgullyrest/static/
<Directory /home/ubuntu/sportsgullyrest/SportsGullyRest>
    <Files wsgi.py>
        Order deny,allow
        Require all granted
    </Files>
</Directory>

<Directory /home/ubuntu/sportsgullyrest/static>
    Require all granted
</Directory>
<Directory "/usr/local/lib/python2.7/dist-packages/django/contrib/admin/static/admin">
    Order allow,deny
    Options Indexes
    Allow from all
    IndexOptions FancyIndexing
</Directory>

我使用Django 1.7,这是我的设置文件

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
SETTINGS_DIR = os.path.dirname(__file__)
PROJECT_PATH = os.path.join(SETTINGS_DIR, os.pardir)
PROJECT_PATH = os.path.abspath(PROJECT_PATH)
TEMPLATE_PATH = os.path.join(PROJECT_PATH, 'templates')
STATIC_PATH = os.path.join(PROJECT_PATH, 'static')



# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.7/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '...'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

TEMPLATE_DEBUG = True

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = (
    ...
)

MIDDLEWARE_CLASSES = (
   ...
)
AUTHENTICATION_BACKENDS = (
   ...
)
TEMPLATE_CONTEXT_PROCESSORS = (
    ...
)
# Database
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        ...
    }
}

# Internationalization
# https://docs.djangoproject.com/en/1.7/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = None

USE_I18N = True

USE_L10N = True

USE_TZ = False
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.7/howto/static-files/

STATIC_URL = '/static/'
MEDIA_ROOT = 'static/'
MEDIA_URL = '/static/img/'
TEMPLATE_DIRS = (
    TEMPLATE_PATH,
)

STATICFILES_DIRS = (
    STATIC_PATH,
)

1 个答案:

答案 0 :(得分:2)

您必须定义STATIC_ROOT设置,例如/static/admin/

然后,从Apache conf中删除static别名,确保./manage.py collectstatic别名指向staticfiles目录,然后运行<!DOCTYPE html> <html> <body> <!-- 1. The <iframe> (and video player) will replace this <div> tag. --> <div id="player"></div> <script> // 2. This code loads the IFrame Player API code asynchronously. var tag = document.createElement('script'); tag.src = "http://www.youtube.com/iframe_api"; var firstScriptTag = document.getElementsByTagName('script')[0]; firstScriptTag.parentNode.insertBefore(tag, firstScriptTag); // 3. This function creates an <iframe> (and YouTube player) // after the API code downloads. var player; function onYouTubeIframeAPIReady() { player = new YT.Player('player', { height: '390', width: '640', events: { 'onReady': onPlayerReady, 'onStateChange': onPlayerStateChange } }); } var videos = [ { vid: 'nbCqWMIT-Pk', startSeconds: 10, endSeconds: 15 }, { vid: 'sAoJGNF_laA', startSeconds: 10, endSeconds: 15 }, { vid: 'kF_23_XxaHQ', startSeconds: 10, endSeconds: 15 }, { vid: 'zxYp3I0Gyrg', startSeconds: 10, endSeconds: 15 }, { vid: 'U_gA99OQwO4', startSeconds: 10, endSeconds: 15 } ]; var index = 0; // 4. The API will call this function when the video player is ready. function onPlayerReady(event) { event.target.cueVideoById({ videoId: videos[index].vid, startSeconds: videos[index].startSeconds, endSeconds: videos[index].endSeconds }); event.target.playVideo(); } function onPlayerStateChange(event) { if (event.data === YT.PlayerState.ENDED) { console.log(index); if (index < videos.length - 1) { index++; event.target.loadVideoById({ videoId: videos[index].vid, startSeconds: videos[index].startSeconds, endSeconds: videos[index].endSeconds }); } } } </script> </body> </html>