[英]Django Server Static files
I am trying to use the following code to link to the correct image based on the rating. 我正在尝试使用以下代码基于评级链接到正确的图像。 However the server interprets as http://127.0.0.1:8000/static/images/rating-%7B%7Bfeedback.reception_courtesy%7D%7D.gif
instead of http://127.0.0.1:8000/static/images/rating-1.gif
但是,服务器将其解释为http://127.0.0.1:8000/static/images/rating-%7B%7Bfeedback.reception_courtesy%7D%7D.gif
而不是http://127.0.0.1:8000/static/images/rating-1.gif
<img src="{% static 'images/rating-{{feedback.reception_courtesy}}.gif' %}" alt="My image"/>
I am not sure where I am running wrong here. 我不确定我在哪里跑错了。
The problem is that variables are not interpolated within the url parameter of {% static 'url' %}
, so {{feedback.reception_courtesy}}
is taken literally. 问题是变量没有在{% static 'url' %}
的url参数内插值,因此{{feedback.reception_courtesy}}
是按字面意义使用的。
Do it like this: 像这样做:
<img src="{% static 'images' %}/rating-{{feedback.reception_courtesy}}.gif" alt="My image"/>
This works fine because the variable is now outside of {% static ... %}
, and because {% static 'one/two/three' %}
is equivalent to {% static 'one/two' %}/three
之所以可以正常工作,是因为该变量现在不在{% static ... %}
,并且因为{% static 'one/two/three' %}
等效于{% static 'one/two' %}/three
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.