加载动态生成的图像时出现奇怪的 Django/Apache 500 错误(有时)

加载动态生成的图像时出现奇怪的 Django/Apache 500 错误(有时)

我在加载动态生成的图像时遇到了非常奇怪的 Http500 错误。出于某种原因,这种情况只是偶尔发生。apache 错误日志未显示任何错误。django 开发服务器在开发过程中可以很好地为它们提供服务。

日志中的示例:

174.255.117.79 - - [04/Feb/2013:12:00:46 -0600] "GET /images/gender/ HTTP/1.1" 500 531 "http://okstereotype.me/results/" "Mozilla/5.0 (Linux; U; Android 4.0.4; en-us; XT862 Build/IMM76D) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30"
174.255.117.79 - - [04/Feb/2013:12:00:46 -0600] "GET /images/bodytype/ HTTP/1.1" 500 531 "http://okstereotype.me/results/" "Mozilla/5.0 (Linux; U; Android 4.0.4; en-us; XT862 Build/IMM76D) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30"
174.255.117.79 - - [04/Feb/2013:12:00:46 -0600] "GET /images/religion/ HTTP/1.1" 200 30722 "http://okstereotype.me/results/" "Mozilla/5.0 (Linux; U; Android 4.0.4; en-us; XT862 Build/IMM76D) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30"
174.255.117.79 - - [04/Feb/2013:12:00:46 -0600] "GET /images/smoking/ HTTP/1.1" 200 58577 "http://okstereotype.me/results/" "Mozilla/5.0 (Linux; U; Android 4.0.4; en-us; XT862 Build/IMM76D) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30"
174.255.117.79 - - [04/Feb/2013:12:00:47 -0600] "GET /images/ethnicities/ HTTP/1.1" 500 531 "http://okstereotype.me/results/" "Mozilla/5.0 (Linux; U; Android 4.0.4; en-us; XT862 Build/IMM76D) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30"
~

有时它们会加载:

192.168.1.4 - - [04/Feb/2013:12:11:20 -0600] "GET /images/gender/ HTTP/1.1" 500 531 "http://okstereotype.me/results/" "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:18.0) Gecko/20100101 Firefox/18.0"
192.168.1.4 - - [04/Feb/2013:12:11:20 -0600] "GET /images/religion/ HTTP/1.1" 200 41967 "http://okstereotype.me/results/" "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:18.0) Gecko/20100101 Firefox/18.0"
192.168.1.4 - - [04/Feb/2013:12:11:20 -0600] "GET /images/ethnicities/ HTTP/1.1" 200 44148 "http://okstereotype.me/results/" "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:18.0) Gecko/20100101 Firefox/18.0"
192.168.1.4 - - [04/Feb/2013:12:11:20 -0600] "GET /images/bodytype/ HTTP/1.1" 200 44471 "http://okstereotype.me/results/" "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:18.0) Gecko/20100101 Firefox/18.0"
192.168.1.4 - - [04/Feb/2013:12:11:20 -0600] "GET /images/smoking/ HTTP/1.1" 200 59910 "http://okstereotype.me/results/" "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:18.0) Gecko/20100101 Firefox/18.0"

图像是通过以下视图生成的:

(r'^images/(\w+)/$', mysite.views.show_pic)

视图使用 PIL 库来绘制和保存图像,如下所示:

response = HttpResponse(mimetype="image/png")
img.save(response, "PNG")

不过,我的所有静态 CSS 均已正确提供。有什么想法吗?

答案1

分析错误的一个好方法是复制一份 settings.py,例如将其命名为“settings_test.py”。然后,将此副本设置为 DEBUG=True,并从 shell 运行 -您可以使用除 80 或 443 以外的其他端口

python manage.py runserver 0.0.0.0:5678 --settings relative.path.to.settings_test

或者:

python django-admin.py runserver 0.0.0.0:5678 --settings relative.path.to.settings_test

然后,通过此端口进行测试(例如:www.mydomain.com:5678/),如果发生某些事情,您将看到回溯。即使它是不会干扰主页加载的辅助 GET/POST 方法,您也应该会在 Shell 中看到这样的回溯。

完成后使用 Ctrl+C 中断 runserver 的执行。

相关内容