Jinja2 KeyError,IndexError异常处理

时间:2019-03-06 16:11:03

标签: python-3.x exception jinja2

几天来,我一直在使用jinja2的自定义过滤器,但是我不知道它如何解决我的问题。

#Counter({2: 4, 1: 1, 9: 1, 8: 1})This is the sample dict that i want to 
access
#s_num = {1:["","man","girl","nam"],2:["","A","B","C"],9:["","D","E","F"]}

In main.py, I wrote the code as below
# number of time a number occured
for k, v in d.items():
try:
    bash = (s_num[k][v])
except (KeyError, IndexError):
    pass 

In my template, I wrote
{{ bash }}#output man

它仅适用于一项,但我希望它列出字典中所有适用的内容。拜托,我需要你的帮助。

0 个答案:

没有答案