Heroku找不到静态文件Django
我知道也有类似的问题,但相信我,我已经检查了每一个问题,但仍然没有检查我的静态文件。 非常感谢您的帮助 我的所有设置和其他代码如下 当我运行Heroku找不到静态文件Django,django,heroku,django-staticfiles,Django,Heroku,Django Staticfiles,我知道也有类似的问题,但相信我,我已经检查了每一个问题,但仍然没有检查我的静态文件。 非常感谢您的帮助 我的所有设置和其他代码如下 当我运行 $heroku运行python manage.py collectstatic 以及部署应用程序时获得的erorr setting.py: import os import dj_database_url # this is because I have my setting.py under setting folder BASE_DIR = os.
$heroku运行python manage.py collectstatic
以及部署应用程序时获得的erorr setting.py:
import os
import dj_database_url
# this is because I have my setting.py under setting folder
BASE_DIR = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
SECRET_KEY = os.environ['secret_key']
DEBUG = False
ADMINS = []
ALLOWED_HOSTS = ['*']
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = '...@gmail.com'
EMAIL_HOST_PASSWORD = os.environ['EMAIL_HOST_PASSWORD']
EMAIL_PORT = 587
EMAIL_USE_TLS = True
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.sites',
'registration',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
# third party apps
'crispy_forms',
# my apps
'home',
]
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',
]
ROOT_URLCONF = 'site2.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.template.context_processors.i18n',
'django.template.context_processors.media',
'django.template.context_processors.static',
'django.template.context_processors.tz',
'django.contrib.messages.context_processors.messages',
],
},
},
]
WSGI_APPLICATION = 'site2.wsgi.application'
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
db_from_env = dj_database_url.config()
DATABASES['default'].update(db_from_env)
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',
},
]
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
STATIC_URL = '/static/'
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static')]
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'
# registration settings
ACCOUNT_ACTIVATION_DAYS = 7
REGISTRATION_AUTO_LOGIN = True
SITE_ID = 1
LOGIN_REDIRECT_URL = '/'
# crispy form settings
CRISPY_TEMPLATE_PACK = 'bootstrap3'
程序文件:
web: gunicorn site2.wsgi --log-file -
wsgi.py:
import os
from django.core.wsgi import get_wsgi_application
from whitenoise.django import DjangoWhiteNoise
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "site2.settings")
application = get_wsgi_application()
application = DjangoWhiteNoise(application)
URL.py:
from django.conf.urls import url, include
from django.contrib import admin
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^accounts/', include('registration.backends.default.urls')),
url(r'^', include('home.urls')),
]
if not settings.DEBUG:
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
以下是未正确加载的我的模板的一部分:
<!DOCTYPE html>
{% load static from staticfiles %}
<html lang="en">
<head>
<!-- Custom styles for this template -->
<link href="{% static 'css/offcanvas.css' %}" rel="stylesheet">
</head>
{%load static from staticfiles%}
URL.py中的更改:
if settings.DEBUG is True:
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
或者删除URL中的静态服务代码,因为whitenose正在服务静态文件,您不需要在heroku中从django服务静态文件。尝试{%load static%}而不是{%load static from staticfiles%}不成功!还是不行!我注释掉了url中的静态服务代码,并再次将everyhtin推送到heroku,我看到当我运行$heroku时,它会处理静态文件,运行python manage.py collectstatic[后处理的'js/googlemap.js'作为'js/googlemap.fbc11c53299d.js',但它仍然无法在我的应用程序中找到.js或.css请更改
STATICFILES\u DIRS=[os.path.join](BASE_DIR,'static')]
toSTATICFILES_DIRS=[os.path.join(BASE_DIR,'static'),]
清除缓存和硬刷新可能吗?我不太清楚……非常感谢您的帮助。@user5772094如果您可以避免后期处理,看看会发生什么?或者手动将文件放入静态目录,尝试使用/static正常访问它/