Django在模板标签中使用变量 [英] Django use a variable within a template tag
本文介绍了Django在模板标签中使用变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在Django模板中使用静态模板标签:
I am using the static template tag in my Django template:
{% load staticfiles %}
<img src="{% static "my_app/myexample.jpg" %}" alt="My image"/>
但不是my_app / myexample.jpg
,我需要使用一个文件名,该文件名是我的模型对象的属性(即 {{SampleModel.0.propertyValue}}
,我将其作为上下文传递这个模板,但是如何在静态模板标签中包含 {{...}}
?它会引发错误。
But instead of "my_app/myexample.jpg"
, I need to use a file name that is a property of my model object (i.e. {{ SampleModel.0.propertyValue }}
, which I am passing as context to this template. But how do I include {{ ... }}
within the static template tag? It throws an error.
任何出路?
推荐答案
您可以在静态模板标签中使用变量,需要 {{
或}}
You can use use a variable in the static template tag. Note that you don't need the {{
or }}
{% static SampleModel.0.propertyValue %}
这篇关于Django在模板标签中使用变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文