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屋!
查看全文