python - Django 找不到 static/的内容

标签 python django angularjs heroku django-staticfiles

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

这是我的设置.py

设置.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 有什么 super 问题吗?

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

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

最佳答案

debugFalse 时,Django 将不会提供静态文件,因为 View 效率低下并且可能不安全,因此您必须配置您的网络服务器。请参阅 Django 手册中的“部署静态文件”。

关于python - Django 找不到 static/的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29638150/

相关文章:

python - 动态添加字段的 django 表单集可以具有持久数据吗?

mysql - Django和mysql在不同服务器上的性能

html - 在 Visual Studio Code 中为 django-html 文件添加格式化程序(美化器)

angularjs - Angular 可重用函数

python - 从 DataFrame 列构造 Series 时丢失值

python - 如何在 scikit-learn 中实现可调用距离度量?

python - Pipenv 无法安装软件包

python - 在Python3.6上运行的Flask找不到htmlentitydefs?

javascript - 未知提供者 : $uploadProvider <- $upload

AngularJS 模型自省(introspection)/反射