检查Jinja2模板中的Python dict中是否存在密钥 [英] Check if key exists in a Python dict in Jinja2 templates

查看:683
本文介绍了检查Jinja2模板中的Python dict中是否存在密钥的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个python字典:

I have a python dictionary:

settings = {
   "foo" : "baz",
   "hello" : "world"
}

此变量设置然后在Jinja2模板中可用。

This variable settings is then available in the Jinja2 template.

我想检查设置中是否存在密钥 myProperty dict在我的模板中,如果是这样采取一些措施:

I want to check if a key myProperty exists in the settings dict within my template, and if so take some action:

{% if settings.hasKey(myProperty) %}
   takeSomeAction();
{% endif %}

相当于 hasKey

What is the equivalent of hasKey that I can use?

推荐答案

这个工作很好

{%if settings.myProperty%}

这篇关于检查Jinja2模板中的Python dict中是否存在密钥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆