繁体   English   中英

不能在Heroku中使用django-compress

[英]Can't use django-compress with Heroku

我有一个部署到Heroku的Django 1.9.6站点。 DEBUG=False我收到服务器错误(500)。 日志中没有有用的信息,因此我尝试使用DEBUG=True运行它。 现在工作正常。 认为问题可能与我的scss文件处理有关,这确实使我感到困惑,而且我一直在努力。 我最近(除其他事项外)在我的设置文件中添加了COMPRESS_OFFLINE = True ,并进行注释似乎可以缓解问题(尽管随后我的scss文件不起作用)。

我的一些静态settings.py 让我知道您是否需要更多-这对我来说是个谜。 我试图按照这个是最好的,我可以。

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
MEDIA_URL = "/media/"
MEDIA_ROOT = os.path.join(BASE_DIR, "media/")
    STATICFILES_FINDERS = (
        'django.contrib.staticfiles.finders.FileSystemFinder',
        'django.contrib.staticfiles.finders.AppDirectoriesFinder',
        # other finders..
        'compressor.finders.CompressorFinder',
    )

    STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'

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

urls.py

urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

urlpatterns += [
    url(r'^media/(?P<path>.*)$', serve, {
        'document_root': settings.MEDIA_ROOT
    }),
]

urlpatterns += staticfiles_urlpatterns()

编辑:

我已经开始记录日志,并且已经确认这是一个压缩错误。 我收到错误消息:

Internal Server Error: /

OfflineGenerationError at /
You have offline compression enabled but key "171c3b7763dbc51a465d996f7d920cf5" is missing from offline manifest. You may need to run "python manage.py compress".

除了运行建议的命令可以解决该问题外,这与我在本地获得的效果相同。 运行heroku run python manage.py compress没有效果(尽管没有错误运行)

由compress生成的清单存储在我的.gitignore中,因此生产中的清单是陈旧的。 将其添加到git存储库中修复了所有问题。

首次关闭ALLOW_HOSTS设置值,关闭调试后不能为空。

ALLOWED_HOSTS = ['.mydomain.com', '.2nddomain.com']

因为您使用压缩插件:SET

COMPRESS_ENABLED = True
COMPRESS_OFFLINE = True

# this where the collectstatic and compress result output
# point your static alias to here 
STATIC_ROOT = os.path.join(BASE_DIR, 'static')

# in your production env: activate ur virtual environment then run the compress statics command
python manage.py compress
python manage.py collectstatic

出于安全原因,当Debug关闭时,所有异常均被抑制,请在设置文件中设置admin email,以让django通过电子邮件发送所有未捕获的异常

SERVER_EMAIL = 'ur@from-email-address.com'
ADMINS = (
    ('Exceptions Email', 'destination@email.com'),
)

将其添加到loggers部分内的settings.py ,它应该为您提供更多信息(这有助于我解决相同的问题)。

"django.request": {
  "handlers": ["console"],
  "level": "ERROR",
  "propagate": True
}

对于它的价值,这是我类似的settings.py设置:

MEDIA_URL = "http://%s.s3.amazonaws.com/" % (AWS_STORAGE_BUCKET_NAME)
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
STATIC_ROOT = 'staticfiles'
STATIC_URL = os.getenv("DJANGO_STATIC_HOST", "") + "/static/"
if DEBUG:
  STATIC_URL = "/static/"
STATICFILES_DIRS = (
  os.path.join(BASE_DIR, 'static'),
)

注意:我没有MEDIA_ROOTSTATICFILES_FINDERS而且我还在CloudFront使用Whitenoise进行静态文件处理

今天,我试图与“ PythonAnywhere”共享一个网站。 我遇到了同样的问题,并已解决“ Allowed_Host”的问题。

https://docs.djangoproject.com/zh/1.10/ref/settings/#allowed-hosts

settings.py

 ALLOWED_HOSTS = ['*']

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM