gpt4 book ai didi

python - 找不到 heroku 本地静态

转载 作者:太空狗 更新时间:2023-10-30 02:02:57 25 4
gpt4 key购买 nike

“heroku local”找不到静态文件,但“python manage.py runserver”可以毫无问题地找到静态文件。你能帮我解决这个问题吗?

settings.py 内容如下:

PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))
STATIC_ROOT = os.path.join(PROJECT_ROOT, 'static')
STATIC_URL = '/static/'
STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'

我运行 collectstatic 并将静态文件复制到 STATIC_ROOT:

python manage.py collectstatic

manage.py runserver' 毫无问题地找到静态文件:

python manage.py runserver

虽然“heroku local”返回警告“未找到”:

11:52:04 AM web.1 |  [19/May/2016 10:52:04] WARNING [django.request:170] Not Found: /static/admin/css/base.css
11:52:04 AM web.1 | [19/May/2016 10:52:04] WARNING [django.request:170] Not Found: /static/admin/css/base.css
11:52:04 AM web.1 | [19/May/2016 10:52:04] WARNING [django.request:170] Not Found: /static/admin/css/login.css
11:52:04 AM web.1 | [19/May/2016 10:52:04] WARNING [django.request:170] Not Found: /static/admin/css/login.css

不确定这里发生了什么。 'heroku local' 和 'manage.py runserver' 都应该看到静态文件。

注意:我正在使用 Django==1.8.2 和 settings.py 包含:

import os
import dj_database_url

BASE_DIR = os.path.dirname(os.path.dirname(__file__))
PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))

SECRET_KEY = 'secretkey'

DEBUG = True

ALLOWED_HOSTS = []

INSTALLED_APPS = (
# 'django.contrib.sites',
'django.contrib.admin',
'registration',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'dbbackup',
'listable',
'rest_framework',
'bootstrap3',
'django_crontab',
)

ACCOUNT_ACTIVATION_DAYS = 7 # One-week activation window; you may, of course, use a different value.
REGISTRATION_AUTO_LOGIN = True # Automatically log the user in.

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',
'django.middleware.security.SecurityMiddleware',
)

ROOT_URLCONF = 'track.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.contrib.messages.context_processors.messages',
],
},
},
]

WSGI_APPLICATION = 'track.wsgi.application'

LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True

FIXTURE_DIRS = (
os.path.join(BASE_DIR, 'fixtures'),
)

MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = 'media/'

REST_FRAMEWORK = {
# Use Django's standard `django.contrib.auth` permissions,
# or allow read-only access for unauthenticated users.
'DEFAULT_PERMISSION_CLASSES': [
'rest_framework.permissions.DjangoModelPermissionsOrAnonReadOnly'
],
'DEFAULT_PARSER_CLASSES': (
'rest_framework_xml.parsers.XMLParser',
),
'DEFAULT_RENDERER_CLASSES': (
'rest_framework_xml.renderers.XMLRenderer',
),
}

DATABASES = {'default' : dj_database_url.config(default='postgres://testuser:testpw@localhost:5432/testdb')}

STATIC_ROOT = os.path.join(PROJECT_ROOT, 'static')
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)

STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'

BOOTSTRAP3 = {
'include_jquery': True,
}

最佳答案

对我有效的解决方案(24 小时后):

1) 安装最新的稳定版本的 whitenoise(我是从一个旧的例子开始工作的,它需要 whitenoise==2.0.6)

2) 确保将白噪声添加到 MIDDLEWARE_CLASSES:

MIDDLEWARE_CLASSES = (
'whitenoise.middleware.WhiteNoiseMiddleware',
)

注意:使用 whitenoise==2.0.6 时在中间件类中添加 'whitenoise.middleware.WhiteNoiseMiddleware' 会报错。需要升级到最新的稳定版(目前whitenoise==3.1)

关于python - 找不到 heroku 本地静态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37228815/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com