Python 为什么可以';当其他静态文件在源代码中可用时,我是否在django项目中添加静态文件?

Python 为什么可以';当其他静态文件在源代码中可用时,我是否在django项目中添加静态文件?,python,django,heroku,static,local,Python,Django,Heroku,Static,Local,我正在尝试为Heroku构建一个Django应用程序,并且已经完成了投票和教程 使用heroku local或python3 manage.py runserver为基本的Django heroku应用程序提供服务时,我可以看到一些静态文件加载得很好(/static/lang-logo.png)。但是,当我添加main.css并运行python3 manage.py collectstatic时,css文件永远不会显示,在我的index.html中的导入也永远不会加载 我已经准备好,whiten

我正在尝试为Heroku构建一个Django应用程序,并且已经完成了投票和教程

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

我已经准备好,whitenoise最适合在django应用程序中提供静态文件,所以我正在使用它,并根据这里对类似问题的许多回答设置了settings.py

Collectstatic似乎正在工作(见下文)。几天来,我一直在用我的脑袋来思考这个问题,在这里我已经讨论了几十个相关的问题,但似乎没有什么能解决这个问题。我已经到了互联网的尽头,现在你是我唯一的希望。下面是我的代码,如果我意识到我遗漏了一些相关内容,我会更新问题

设置.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' %}" />
收集静态输出

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.

STATIC\u URL
更改为
/STATIC/
,出于测试目的,只需设置
STATICFILES\u DIR=['hello/STATIC/hello']
。Whitenoise不一定要排在第二位。我在底部运行我的应用程序,它与我在Heroku上的Django多租户应用程序配合得非常好。我相信当你现在推到Heroku时,你可能需要再做一次
collectstatic
。如果它在第一次构建后抱怨你的静态文件,就像Heroku说的那样禁用它


编辑:顺便说一下,Heroku不支持SQLite,您需要迁移到postgres。这是我做过的事,从来没有回头看。您所要做的就是更改数据库信息和引擎,然后就可以开始了。

好的,可能无法解决您的问题,但我注意到您的WhiteNoise中间件位于错误的位置。它必须是列表中的第二个,而不是最后一个。这可以改变一切。太棒了!我很有信心。
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.