1

在过去的几个小时里,我一直在通过 Stack Overflow 搜索修复,但大多数关于服务器错误 500 的帖子都无法为我提供修复。Django 找不到静态图片并返回 500。图片在 static/css/images 中。

例如,我尝试获取https://monkeyparliament.herokuapp.com/about/。日志返回:

2019-02-10T17:09:33.362724+00:00 app[web.1]: ValueError: Missing staticfiles manifest entry for 'css\images\donate.png'
2019-02-10T17:09:33.363611+00:00 app[web.1]: 10.31.121.50 - - [10/Feb/2019:17:09:33 +0000] "GET /about/ HTTP/1.1" 500 27 "https://monkeyparliament.herokuapp.com/music/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.96 Safari/537.36"

但是当我https://monkeyparliament.herokuapp.com/music/时,它似乎可以在静态文件夹中找到 css/js/fonts 。随意检查页面源。

我的静态图像由 WhiteNoise ( http://whitenoise.evans.io/en/stable/ ) 提供。正如您在上面看到的,WhiteNoise 在要求中。settings.py 中的 MIDDLEWARE 在 django.middleware.security.SecurityMiddleware 下方添加了“whitenoise.middleware.WhiteNoiseMiddleware”。

为什么找不到我的图片?

档案

web: gunicorn websitemp.wsgi:application --log-file -

要求.txt

dj-database-url==0.5.0
Django==2.0.10
gunicorn==19.9.0
psycopg2==2.7.7
pytz==2018.9
whitenoise==4.1.2

结构

website是应用程序,websitemp是项目

在此处输入图像描述

Github 上的项目

如果您想查看完整结构,您可以在 github 上找到所有文件:https ://github.com/DennisVerstappen/websitempdjango

提前致谢!

4

2 回答 2

0

Create a empty folder staticfiles in your main directory.

Afterwards run python manage.py collectstatic

After that it should be filled with all static assets from all applications.

Note: To keep things seperated you can actually, move your static folder into the Website-Application - Should not make a difference

于 2019-02-11T13:08:31.603 回答
0

谢谢你的思考。在尝试了您的解决方案后,我注意到斜线

{% static 'css\images\donate.png' %} 

不同于日志中其他 Python 文件中的斜线。将反斜杠更改为正斜杠解决了我的问题。我使用的文件夹系统表示法(Windows)与 Heroku 使用的不同。使用下面的符号有效。

{% static 'css/images/donate.png' %}
于 2019-02-20T09:04:30.937 回答