Django找不到静态内容/

时间:2015-04-14 22:02:04

标签: python django angularjs heroku django-staticfiles

我正在尝试将应用程序部署到Heroku,但它无法找到我的JS / CSS文件。

这是我的settings.py

settings.py

"""
Django settings for the MyApp project.

For more information on this file, see
https://docs.djangoproject.com/en/1.7/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.7/ref/settings/
"""

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))


# 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 = SUPER SECRET

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

TEMPLATE_DEBUG = DEBUG

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'debug_toolbar',
    'rest_framework',
    'compressor',
    'authentication',
)

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',
)

ROOT_URLCONF = 'MyApp.urls'

WSGI_APPLICATION = 'MyApp.wsgi.application'


# Database
# https://docs.djangoproject.com/en/1.7/ref/settings/#databases

import dj_database_url

DATABASES = {
    'default': dj_database_url.config(
        default='sqlite:///' + os.path.join(BASE_DIR, 'db.sqlite3')
    )
}

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

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


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

STATIC_URL = '/static/'
STATIC_ROOT = 'staticfiles'

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'dist/static'),
    os.path.join(BASE_DIR, 'static'),
)

STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
    'compressor.finders.CompressorFinder',
)

COMPRESS_ENABLED = True

TEMPLATE_DIRS = (
    os.path.join(BASE_DIR, 'templates'),
)

REST_FRAMEWORK = {
    'DEFAULT_AUTHENTICATION_CLASSES': (
        'rest_framework.authentication.SessionAuthentication',
    )
}

# Honor the 'X-Forwarded-Proto' header for request.is_secure()
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')

# Allow all host headers
ALLOWED_HOSTS = ['*']

AUTH_USER_MODEL = 'authentication.Account'

if not DEBUG:
    # Parse database configuration from $DATABASE_URL
    import dj_database_url
    DATABASES['default'] =  dj_database_url.config()

    # Enable Connection Pooling
    DATABASES['default']['ENGINE'] = 'django_postgrespool'


    # Simplified static file serving.
    # https://warehouse.python.org/project/whitenoise/
    STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'


    # Honor the 'X-Forwarded-Proto' header for request.is_secure()
    SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')

这是包含我想要提供的CSS的文件:

/static/templates/stylesheets.html

{% load compress %}
{% load static %}

{% compress css %}
<link rel="stylesheet" type="text/css" href="{% static "bower_components/bootstrap/dist/css/bootstrap.css" %}" />
<link rel="stylesheet" type="text/css" href="{% static "bower_components/bootstrap-material-design/dist/css/material.css" %}" />
<link rel="stylesheet" type="text/css" href="{% static "bower_components/bootstrap-material-design/dist/css/ripples.min.css" %}" />
<link rel="stylesheet" type="text/css" href="{% static "bower_components/ngDialog/css/ngDialog.css" %}" />
<link rel="stylesheet" type="text/css" href="{% static "bower_components/ngDialog/css/ngDialog-theme-default.css" %}" />

<link rel="stylesheet" type="text/css" href="{% static "lib/snackbarjs/snackbar.min.css" %}" />

<link rel="stylesheet" type="text/css" href="{% static "stylesheets/styles.css" %}" />
{% endcompress %}

我的settings.py是否存在超级错误?

/static/存在于我的目录的根目录中,并且包含HTML文件中的所有文件。

如果需要更多信息,请告知我们。

2 个答案:

答案 0 :(得分:0)

debugFalse时,Django无法提供静态文件,因为视图效率低且可能不安全,因此您必须配置您的网络服务器。请参阅&#34;部署静态文件&#34;在Django手册中。

答案 1 :(得分:-1)

来自文档https://docs.djangoproject.com/en/1.8/ref/settings/#std:setting-STATIC_ROOT

  

绝对路径到collectstatic将收集静态文件以进行部署的目录。