在Django中加载具有变量名的静态文件


问题内容

我正在尝试加载以下静态文件

 <a href="{%static 'static/images/'{{ image.title }}'.png' %}">img file</a>

在从数据库派生image的for循环中images

但是我只是犯了一个错误 Could not parse the remainder: '{{' from ''static/matrices/'{{'

我应该怎么做才能解决这个问题?我不能使用相对路径,因为同样的html模板也将由小节使用。


问题答案:

引号太多!

只是

<a href="{% static 'images/{{ image.title }}.png' %}">img file</a>

并且您不必在链接中调用static,因为您已经加载了static