【发布时间】:2015-11-06 16:28:41
【问题描述】:
在我的主项目目录中,我有一个设置目录,其中包含以下文件:local.py、base.py、production.py 和 __init__.py。运行时 collectstatic 文件保存在项目目录下的文件夹 staticfiles 中
local.py
STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.8/howto/static-files/
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)
PROJECT_DIR = os.path.dirname(__file__)
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
base.py
STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.8/howto/static-files/
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)
STATIC_URL = '/static/'
PROJECT_DIR = os.path.dirname(__file__)
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
production.py
import os
from django.conf import settings
DEBUG = False
TEMPLATE_DEBUG = True
# Parse database configuration from $DATABASE_URL
import dj_database_url
DATABASES['default'] = dj_database_url.config()
# Honor the 'X-Forwarded-Proto' header for request.is_secure()
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
# Allow all host headers
ALLOWED_HOSTS = ['*']
# Static asset configuration
import os
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
STATIC_ROOT = '/static/'
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'staticfiles'),
)
当我运行以下命令时:
heroku run python manage.py collectstatic --noinput
我收到错误消息:You're using the staticfiles app without having set the STATIC_ROOT setting to a filesystem path.
【问题讨论】:
-
完全按照错误消息中的说明进行操作。
STATIC_ROOT是你文件系统上的一个目录,你已经把它设置为一个 web 目录。你的意思是STATIC_ROOT = os.path.join(BASE_DIR, 'static')? -
下面的方法是这样的:
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles') STATIC_URL = '/static/' STATICFILES_DIRS = ( os.path.join(BASE_DIR, 'static'), ) -
我个人使用 STATIC_ROOT = normpath(join(SITE_ROOT, 'assets')),但看起来你有 TBA 的建议是对的。另外,看看Django Whitenoise
标签: python django heroku production