简体   繁体   中英

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

<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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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