【问题标题】:Django static files and HerokuDjango 静态文件和 Heroku
【发布时间】:2015-11-06 16:28:41
【问题描述】:

在我的主项目目录中,我有一个设置目录,其中包含以下文件:local.pybase.pyproduction.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


【解决方案1】:

在 Heroku 上部署的一个很好的模板/指南是 here

看看他们的 settings.py 到最后::

BASE_DIR = os.path.dirname(os.path.abspath(__file__))
STATIC_ROOT = 'staticfiles'
STATIC_URL = '/static/'

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'static'),
)

# Simplified static file serving.
# https://warehouse.python.org/project/whitenoise/
STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'

【讨论】:

  • 感谢您的回答。但是,它仍然不起作用。它给了我同样的错误。
【解决方案2】:

不要在您的 production.py 文件的 STATIC_ROOT 中使用相对路径。

像在 base.py 中一样使用它

STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')

【讨论】:

    猜你喜欢
    • 2023-03-20
    • 2018-09-10
    • 2014-02-04
    • 2016-04-16
    • 2015-05-11
    • 2021-06-03
    • 2017-07-12
    • 2021-05-30
    • 1970-01-01
    相关资源
    最近更新 更多