我有一个django模板,我在其中为模板变量赋值,但我需要使用该值来访问特定对象。即:
type = "MyType1"
data = models.type
是否有我不知道的标签/过滤器?感谢。
真的在我想做的模板中:
{
"models": {
"MyType1": { "data": "somemoredata" },
"MyType2": { "data": "somemoredata" },
"MyType3": { "data": "somemoredata" },
}
}
我的数据看起来像(json):
UIImagePickerController
答案 0 :(得分:0)
Not directly, but you can make a custom filter and do something like:
@register.filter(name='getattr')
def get_a_field(obj_id, attr_str):
my_obj = MyModel.objects.get(id=obj_id)
return getattr(my_obj, attr_str)
{% with operations.type as type %}
{{ models|getattr:type }}
{% endwith %}