python - 为什么当源中有其他静态文件时,我不能在我的 django 项目中添加静态文件?
问题描述
我正在尝试为 Heroku 构建一个 Django 应用程序,并且已经阅读了 Polls 教程和Heroku 文档
当为基本的 Django-heroku 应用程序提供服务时,heroku local
或者python3 manage.py runserver
我可以看到一些静态文件加载得很好(特别是 /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())
索引.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.
解决方案
将您的更改STATIC_URL
为/static/
并出于测试目的,只需设置您的STATICFILES_DIR = ['hello/static/hello']
. Whitenoise 不一定要排在第二位。我在底部运行我的,它与我在 Heroku 上的 Django 多租户应用程序配合得非常好。我相信当你现在推送到 Heroku 时,你可能不得不再做collectstatic
一次。如果它在第一次构建后抱怨你的静态文件,只需像 Heroku 所说的那样禁用它。
编辑:顺便说一句,Heroku 不支持 SQLite,您需要迁移到 postgres。这是我做过的事情,从未回头。您所要做的就是更改数据库信息和引擎,一切顺利。
推荐阅读
- angular - 使用惰性加载就地绑定 ngFor
- javascript - 如何在 VueJs 项目中导入单个 Lodash 函数?
- php - Apache 重写规则导致 JS 和 CSS 文件的“重定向过多”
- tsql - T-SQL:如果不存在则插入
- python - 熊猫在唯一值上迭代一列并获取另一列的值
- ansible - Ansible 模板循环
- flutter - 在颤振中,按下后退按钮时如何返回上一个 URL?
- python - 有缩进错误
- azure - Azure - 在设置之前为 VM 分配托管标识访问权限
- javascript - 我的可重用函数中的这个 JS 字符串是否太复杂而无法工作?