简体   繁体   English

Django static 文件在 Heroku 上使用 ZC5FD214CDD0D2B3B54272E73B0 部署时未加载

[英]Django static files are not loaded when deploying on Heroku using Docker and Whitenoise

I'm trying to deploy a django app on Heroku (using Whitenoise ), and all works fine except for the static files.我正在尝试在 Heroku 上部署 django 应用程序(使用Whitenoise ),除了 static 文件外,其他一切都很好。 The sample project just shows a header "Home" on the front page.示例项目仅在首页显示了 header“主页”。 If the css file is loaded correctly then the header will have the red colour, otherwise it will be black.如果正确加载 css 文件,则 header 将显示为红色,否则为黑色。

I got the above project to work in a virtualenv , a Docker container, with DEBUG=False , but I could not make it work on Heroku for some reason (The header would be Red if the CSS loaded correctly). I got the above project to work in a virtualenv , a Docker container, with DEBUG=False , but I could not make it work on Heroku for some reason (The header would be Red if the CSS loaded correctly).

I tried to use the django-heroku as suggested by the Heroku docs but it starts throwing 500 server error, and when checking the heroku logs it says manifest not found for the css file , which seems to be because it sets STATICFILES_STORAGE to a different value. I tried to use the django-heroku as suggested by the Heroku docs but it starts throwing 500 server error, and when checking the heroku logs it says manifest not found for the css file , which seems to be because it sets STATICFILES_STORAGE to a different value .

Is there something setup incorrectly in the settings or the yml files? 设置yml文件中是否存在错误设置? Or perhaps because I'm deploying a Docker file (as opposed to a Procfile ) I need to use different settings?或者可能是因为我正在部署 Docker 文件(而不是Procfile )我需要使用不同的设置?

Update 1:更新 1:

As suggested in the comments.正如评论中所建议的那样。 I tried to follow what's done in this tutorial , which has two additional things done:我尝试按照本教程中的内容进行操作,其中还完成了两件事:

  1. Setting STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'设置STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
  2. Running heroku plugins:install @heroku-cli/plugin-manifest运行heroku plugins:install @heroku-cli/plugin-manifest

When doing this, now I get a different HTTP 500 error:这样做时,现在我得到一个不同的 HTTP 500 错误:

/usr/local/lib/python3.7/site-packages/whitenoise/base.py:115: UserWarning: No directory at: /django-heroku-staticfiles/staticfiles/ /usr/local/lib/python3.7/site-packages/whitenoise/base.py:115:UserWarning:没有目录:/django-heroku-staticfiles/staticfiles/

Now I think the problem is clear.现在我认为问题很清楚了。 So I started running some commands on the heroku instance:所以我开始在 heroku 实例上运行一些命令:

$ heroku run ls -l /django-heroku-staticfiles
Running ls -l /django-heroku-staticfiles on ⬢ evening-lowlands-39080... up, run.5553 (Free)
total 160
-rw------- 1 u35232 dyno    179 May 30 12:21 Dockerfile
-rw------- 1 u35232 dyno 131072 May 30 12:21 db.sqlite3
-rw------- 1 u35232 dyno    235 May 30 12:21 docker-compose.yml
drwx------ 2 u35232 dyno   4096 May 30 12:21 hello_project
-rw------- 1 u35232 dyno    182 May 30 12:21 heroku.yml
-rwx------ 1 u35232 dyno    633 May 30 12:21 manage.py
drwx------ 4 u35232 dyno   4096 May 30 12:21 pages
-rw------- 1 u35232 dyno     93 May 30 12:21 requirements.txt
drwx------ 2 u35232 dyno   4096 May 30 12:21 templates

$ heroku run python manage.py collectstatic --clear --noinput
Running python manage.py collectstatic --clear --noinput on ⬢ evening-lowlands-39080... up, run.9686 (Free)

131 static files copied to '/django-heroku-staticfiles/staticfiles', 413 post-processed.

$ heroku run ls -l /django-heroku-staticfiles
Running ls -l /django-heroku-staticfiles on ⬢ evening-lowlands-39080... up, run.3303 (Free)
total 160
-rw------- 1 u20456 dyno    179 May 30 12:21 Dockerfile
-rw------- 1 u20456 dyno 131072 May 30 12:21 db.sqlite3
-rw------- 1 u20456 dyno    235 May 30 12:21 docker-compose.yml
drwx------ 2 u20456 dyno   4096 May 30 12:21 hello_project
-rw------- 1 u20456 dyno    182 May 30 12:21 heroku.yml
-rwx------ 1 u20456 dyno    633 May 30 12:21 manage.py
drwx------ 4 u20456 dyno   4096 May 30 12:21 pages
-rw------- 1 u20456 dyno     93 May 30 12:21 requirements.txt
drwx------ 2 u20456 dyno   4096 May 30 12:21 templates

Why is staticfiles not getting created?为什么没有创建staticfiles Maybe I need to run the command through Docker?也许我需要通过 Docker 运行命令? Sorry I'm new to Docker, Django, and Heroku, so I might be missing something obvious.抱歉,我是 Docker、Django 和 Heroku 的新手,所以我可能遗漏了一些明显的东西。

Thanks to @Original BBQ Sauce , this SO question , and this Reddit post the problem is finally solved.感谢@Original BBQ Sauce ,这个SO question这个 Reddit 帖子,问题终于解决了。 It's related to where/when we run collectstatic .它与我们运行collectstatic的位置/时间有关。 Initially I was running it as a release command in the heroku.yml file, which was not creating the staticfiles directory for some reason.最初,我将它作为heroku.yml文件中的release command运行,该文件由于某种原因没有创建 staticfiles 目录。 I was doing it this way as is done in the Django for Professionals book.我是这样做的,就像在 Django for Professionals 书中所做的那样。

Now, if that command is run in the Dockerfile instead, the staticfiles directory is created and all should work fine .现在,如果该命令在 Dockerfile 中运行,则会创建 staticfiles 目录,并且一切正常 The repo now has the new, working settings.存储库现在具有新的工作设置。

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

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