当源中有其他静态文件时,为什么不能在django项目中添加静态文件?

时间:2018-07-16 21:47:46

标签: python django heroku static local

我正在尝试为Heroku构建Django应用,并通过了民意调查教程和Heroku documentation

在使用heroku localpython3 manage.py runserver服务基本的Django-heroku应用程序时,我可以看到一些静态文件已经很好地加载了(特别是/static/lang-logo.png)。但是,当我添加main.css并运行python3 manage.py collectstatic时,css文件将不会显示,并且index.html中的导入也将不会加载。

我已经准备好白噪声最适合在Django应用程序中提供静态文件,因此我正在使用它,并根据对此处类似问题的许多回答设置了settings.py。

Collectstatic似乎正在运行(请参阅下文)。在过去的几天里,我一直在努力解决这个问题,并且在这里经历了数十个相关问题,似乎没有任何事情可以解决。我已经到了互联网的尽头,现在你是我唯一的希望。我的代码在下面,如果我意识到我错过了一些相关的内容,我将更新问题。

Settings.py

"""
Django settings for gettingstarted project.

Generated by 'django-admin startproject' using Django 2.0.

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

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

import os
import django_heroku
import fred

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


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

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'CHANGE_ME!!!! (P.S. the SECRET_KEY environment variable will be used, if set, instead).'

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

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'hello'
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    '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',
    'whitenoise.middleware.WhiteNoiseMiddleware',
]

ROOT_URLCONF = 'gettingstarted.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

WSGI_APPLICATION = 'gettingstarted.wsgi.application'


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

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}


# Password validation
# https://docs.djangoproject.com/en/2.0/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]


# Internationalization
# https://docs.djangoproject.com/en/2.0/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/2.0/howto/static-files/
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATIC_URL = 'hello/static/hello'
STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'hello/static/hello'),
)

django_heroku.settings(locals())

index.html

{% load staticfiles %}
{% load static %}

<link rel="stylesheet" type="text/css" href="{% static 'hello/screener.css' %}" />

目录结构

/hello/static/hello/index.css

收集静态输出

Post-processed 'hello/screener.css' as 'hello/screener.4d9ca1ef5004.css'
Post-processed 'screener.css' as 'screener.4d9ca1ef5004.css'

121 static files copied to '/Users/user/Desktop/heroku-bayesian/python-getting-started/staticfiles', 151 post-processed.

1 个答案:

答案 0 :(得分:0)

STATIC_URL更改为/static/,出于测试目的,只需设置STATICFILES_DIR = ['hello/static/hello']。白噪声不一定需要排第二。我在最下面运行我的游戏,它与Heroku上的Django多租户应用程序配合使用效果很好。我相信,当您现在推送至Heroku时,您可能不得不再次执行collectstatic。如果第一次创建后它抱怨您的静态文件,请像Heroku所说的here一样将其禁用。

编辑:Heroku不支持SQLite,因此您需要迁移到postgres。这是我做过的事,从未回头。您所要做的就是更改数据库信息和引擎,一切顺利。

相关问题