2014-04-28 8 views
0

Django的新手在這裏開始接觸我的第一個應用程序...如何在{%static%}調用中引用動態文件?

我需要我的靜態資產文件夾([項目] /靜態/圖片/ ____。JPG)中引用的圖像和我一直在做這樣使用該{%靜態%}模板標籤類似的例子在入門教程:

{% load staticfiles %} 
<img src="{% static "my_app/image.jpg" %}"> 

不過,我需要用它從view.py文件傳遞一個變量引用圖像文件本身。當然,這個語法不起作用,但應該說明我正在嘗試做什麼。

<img src="{% static "my_app/{{ imagevariable }}" %}"> 

什麼是在靜態模板標籤內引用這個變量的正確語法?

回答

1

with一試:https://docs.djangoproject.com/en/dev/ref/templates/builtins/#with

{% with "myapp/"|add:imagevariable as filename %} 
    <img src="{% static filename %}"> 
{% endwith %} 

您還可以使用get_static_prefix:https://docs.djangoproject.com/en/dev/ref/templates/builtins/#std:templatetag-get_static_prefix

<img src="{% get_static_prefix %}myapp/{{ imagevariable }}"> 
+0

真棒,這並獲得成功!感謝鏈接到文檔。我知道這裏發生了什麼。 – dekaliber

相關問題