Django-使用模板中的变量调用列表或字典项 [英] Django - Calling list or dict item using a variable in template

查看:470
本文介绍了Django-使用模板中的变量调用列表或字典项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用模板中的变量调用模板中的字典或列表对象,而没有结果.

我要尝试的操作与此通用的python代码相同:

keylist=[
        'firstkey',
        'secondkey',
        'thirdkey',
        ]
exampledict={'firstkey':'firstval',
             'secondkey':'secondval',
             'thirdkey':'thirdval',
             }

for key in keylist:
    print(exampledict[key])

#Produces:
    #firstval
    #secondval
    #thirdval

django模板中的内容有些不同. 我有一个定义为key ='firstkey'的变量传递给模板. 如果我想调用同一本词典:

{{ exampledict.firstkey }} #Produces: firstval
{{ exampledict.key }} #Produces: None

Django循环模板也具有一个产生的变量forloop.counter0,从第一个循环中的0增加到最后一个循环中的n-1,这不能调用列表对象. 我有一个清单:

tabletitles=['first table', 'second table', 'third table']

我想循环调用并创建表,将各个表的表标题放在上面,如下所示:

{% for table in tables %}
<h3> first table </h3>
<table>
...
</table>
{% endfor %}

在这种情况下,我想做的是

{% for table in tables %}
<h3> {{ tabletitles.forloop.counter0 }} </h3>
<table>
...
</table>
{% endfor %}

这也不起作用,因为我不能使用单独的变量来为模板中的字典或列表调用对象. 有什么方法可以使它正常工作,或者有更好的方法可以一起完成这些工作?

解决方案

Django模板语言不允许您访问字典键和带有变量的列表.您可以编写一个模板标签来执行此操作(例如,参见此问题),但是在您的情况下,有一个更简单的选择. /p>

在您看来,将表格和标题压缩在一起

tables_and_titles = zip(tables, tabletiles)

然后在模板中一起遍历它们.

{% for table, title in tables_and_titles %}
    {{ title }}
    <table>
    {{ table }}
    </table>
{% endfor %}

I'm trying to call a dictionary or list object in a template using a variable in that template with no results.

What I'm trying to is identical to this general python code:

keylist=[
        'firstkey',
        'secondkey',
        'thirdkey',
        ]
exampledict={'firstkey':'firstval',
             'secondkey':'secondval',
             'thirdkey':'thirdval',
             }

for key in keylist:
    print(exampledict[key])

#Produces:
    #firstval
    #secondval
    #thirdval

Things work a little different in the django template. I have a variable defined as key='firstkey' passed onto the template. If i want to call the same dictionary:

{{ exampledict.firstkey }} #Produces: firstval
{{ exampledict.key }} #Produces: None

Django template for loops also has a produced variable forloop.counter0, increasing from 0 in the firstloop to n-1 in the last loop, this cannot call list objects. I have a list:

tabletitles=['first table', 'second table', 'third table']

And I want to call and create tables in a loop, putting the table titles for the respective tables above like so:

{% for table in tables %}
<h3> first table </h3>
<table>
...
</table>
{% endfor %}

What I would like to do in this case is

{% for table in tables %}
<h3> {{ tabletitles.forloop.counter0 }} </h3>
<table>
...
</table>
{% endfor %}

Which doesn't work either, since I can't use a separate variable to call an object for a dict or list in the template. Is there a way to get this to work, or a better way to do it all together?

解决方案

The Django template language does not let you access dictionary keys and lists with variables. You could write a template tag to do this (see this question for example), but in your case there's a simpler alternative.

In your view, zip your tables and titles together

tables_and_titles = zip(tables, tabletiles)

Then loop through them together in your template.

{% for table, title in tables_and_titles %}
    {{ title }}
    <table>
    {{ table }}
    </table>
{% endfor %}

这篇关于Django-使用模板中的变量调用列表或字典项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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