【发布时间】:2018-10-26 21:17:08
【问题描述】:
我想在我的 django 2.0.5 模板中使用静态文件,该模板位于:
https://my_cloud_front_adress.cloudfront.net/staticfiles/picture_small.jpg
在 Heroku 上我设置了变量:
STATIC_URL = https://my_cloud_front_adress.cloudfront.net/staticfiles/
templates/base.html
{% load static %}
{# this one is NOT working #}
<img src="{% static 'picture_small.jpg' %}" alt="my test image"/>
{# this one is working #}
<img src="https://my_cloud_front_adress.cloudfront.net/staticfiles/picture_small.jpg" alt="my test image"/>
我应该如何设置STATIC_URL 以使其在模板中工作:
<img src="{% static 'picture_small.jpg' %}" alt="my test image"/>
【问题讨论】:
-
使用
{% static ... %}时渲染的src值是多少?要检查这一点,右键单击图像 > 检查。 -
当我在 Heroku 上运行它时,我得到了错误 500。
-
你可以转
DEBUG=TRUE,这样你就可以真正看到错误了。或者,如果您为您的应用启用了错误日志记录,您可以发布错误日志吗? -
Page not found (404) Request Method: GET Request URL: http://myexample.com/static/picture_small.jpg Using the URLconf defined in test7.urls, Django tried these URL patterns, in this order: admin/ The current path, static/picture_small.jpg, didn't match any of these. -
这不是 500 错误。据此,
STATIC_URL的值设置为'/static/'。在您的设置中将此值更改为您想要的值,一切都会正常工作。
标签: django