Heroku

时间:2016-07-11 15:38:06

标签: django heroku django-staticfiles

我已经将Django项目部署为Heroku应用程序,但无法使静态文件生效。

settings.py

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.9/howto/static-files/

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

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

# Extra places for collectstatic to find static files.
STATICFILES_DIRS = (
    os.path.join(PROJECT_ROOT, 'static'),
)

这正是Django and Static Assets

的建议

我已将DISABLE_COLLECTSTATIC设置为false,

$ heroku config:set DISABLE_COLLECTSTATIC=0 --app mathsproject

文件系统如下所示,

enter image description here

表明'静态'目录是我的Django项目的根目录。但我不知道' staticfiles'目录是?这应该在某处创建吗?

运行时,

$ heroku logs

我通常会收到错误,表明缺少静态文件。这在浏览器中也很明显。

......
2016-07-11T07:39:30.187273+00:00 app[web.1]: Not Found: /static/assets/js/jquery-1.10.2.min.js
    2016-07-11T07:39:30.264037+00:00 app[web.1]: Not Found: /static/assets/img/4.png
    2016-07-11T07:39:30.283627+00:00 app[web.1]: Not Found: /static/assets/img/testimonials/1.jpg
........

通过添加到mathProject / wsgi.py

启用了Whitenoise
import os

from django.core.wsgi import get_wsgi_application

from whitenoise.django import DjangoWhiteNoise

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mathsProject.settings")

application = get_wsgi_application()

application = DjangoWhiteNoise(application)

在settings.py的底部,我把

STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'

运行

$ heroku run bash

然后,

$ python manange.py collectstatic --noinput

这给出了几行错误,最后一个错误是

OSError: [Errno 2] No such file or directory: '/app/mathsProject/static'

但是有一个静态的'目录在' mathsProject'

谢谢,

1 个答案:

答案 0 :(得分:0)

The static files are recognised when the 'static' directory is inside the Django project directory, which unfortunately had the same name as the whole Django directory. Both were called 'mathsProject'.

Also I set,

$ heroku config:set DISABLE_COLLECTSTATIC=1

This works for me.