【问题标题】:Can't use django-compress with Heroku无法将 django-compress 与 Heroku 一起使用
【发布时间】:2017-07-04 21:16:09
【问题描述】:

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

我的一些静态settings.py。如果你需要更多,请告诉我——这对我来说是个谜。我尽量关注this

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 没有效果(不过运行没有错误)

【问题讨论】:

  • 您如何提供静态文件?在 Heroku 或其他服务上?
  • 我感觉这与此有关——我对一般的静态文件感到非常困惑。我可能设置错了。我不知道为什么除了我的 scss 文件之外的所有文件都出现了。我主要试图遵循这一点。 devcenter.heroku.com/articles/django-assets 会更新我的问题。
  • 日志不包含任何有用的信息,但您介意分享这些日志吗?
  • 您的网站是否在 DEBUG=False 的情况下在本地运行?
  • @DanielHepper 在这一点上,部分。缺少一些(不是全部)静态资产。我还没有找到与缺失的共同点(所有图像,但一些图像正在加载)

标签: python django heroku


【解决方案1】:

compress 生成的清单存储在我的 .gitignore 中,因此生产中的清单已过时。将其添加到 git 存储库可以解决所有问题。

【讨论】:

    【解决方案2】:

    ALLOW_HOSTS 的第一个偏移值,调试关闭时不能为空。

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

    因为您使用压缩插件: 设置

    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 关闭时,出于安全原因,所有异常都被抑制,在设置文件中设置管理员电子邮件,让 django 电子邮件所有未捕获的异常

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

    【讨论】:

      【解决方案3】:

      将此添加到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,我还在使用 WhitenoiseCloudFront 来处理我的静态文件

      【讨论】:

        【解决方案4】:

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

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

        settings.py

         ALLOWED_HOSTS = ['*']
        

        【讨论】:

        • 我对我的 allowed_hosts 非常有信心(另外,为了安全起见,您应该比 * 更具体)
        • 我这样做是为了摆脱糟糕的服务器错误。我知道这不安全,但我是在项目开发状态下这样做的,只是为了测试。如果我误解了您的问题,请不要责怪。我以为我遇到了同样的问题,所以我给出了自己的解决方案。英语也不好:/
        • 我很感激!这是个好主意,也许会对其他人有所帮助!
        猜你喜欢
        • 1970-01-01
        • 2021-06-09
        • 2016-07-28
        • 2018-03-24
        • 2015-08-17
        • 2011-12-14
        • 1970-01-01
        • 1970-01-01
        • 2018-07-15
        相关资源
        最近更新 更多