Python 在HTML上使用Django词典

Python 在HTML上使用Django词典,python,django,dictionary,Python,Django,Dictionary,想象你在一个视图中有一本这样的词典 CHOICES = ( (0, "Numeric"), (1, "String"), (2, "Boolean"), (3, "Date"), ) 稍后在视图中,我将字典作为上下文传递给模板。像这样: 编辑1: # type is a int type = 2 ctx = {'choices':CHOICES, 'type':dict(type)} 问题

想象你在一个视图中有一本这样的词典

CHOICES = (
             (0, "Numeric"),
             (1, "String"),
             (2, "Boolean"),
             (3, "Date"),
)
稍后在视图中,我将字典作为上下文传递给模板。像这样:

编辑1:

# type is a int
type = 2
ctx = {'choices':CHOICES, 'type':dict(type)}
问题是我不能用HTML解析我的字典。在本例中,我想返回字符串
“Boolean”
以下是我的片段:

{{ choices[type] }}
我怎样才能解决这个问题

我认为有一种解决方案是使用
进行
循环,然后将变量与类型进行比较,但是代码太多了。或者我可以传递字符串而不是整个字典,但这不是我想要的。稍后我会将它与类型数组一起使用,这样就不能使用最后一个解决方案

编辑2:

# type is a int
type = 2
ctx = {'choices':CHOICES, 'type':dict(type)}
嗯。简单一点

在models.py中

class AttributeType(models.Model):
    name = models.CharField("Name", max_length=100)
    description = models.CharField("Description", max_length=100)
    choice = models.IntegerField("Choice")
    def __unicode__(self):
        return self.name
假设我已经用一些随机值填充了数据库

在views.py中:

def list_attribute_types(request):
    attribute_types = AttributeType.objects.all()
    # Here we should add the dict to the ctx (?)
    ctx = {'attribute_types':attribute_types}
    return render_to_response('des/attribute_type/list_attribute_types.html', ctx, context_instance=RequestContext(request))
在“我的列表”\u attribute\u types.html中:

{% extends "index.html" %} {% block title %}SGC - Administración de Tipo
de Atributo{% endblock %} {% block content %}

<div class="jumbotron">
    <div class="bs-example">
        <h1>Administración de Tipo de Atributos</h1>
        <a class="btn btn-sm btn-default" href="{% url 'create_attribute_type' %}"><span
            class="glyphicon glyphicon-plus"></span>Crear Tipo de Atributos</a> <br>
        <br> 
        {% if attribute_types %}
        <div class="table-responsive">
            <table class="table table-hover">
                <thead>
                    <tr>
                        <th>ID</th>
                        <th>Nombre de Tipo de Atributos</th>
                        <th></th>
                        <th></th>
                        <th></th>
                    </tr>
                </thead>
                <tbody>
                    {% for attribute_type in attribute_types %}
                    <tr>
                        <td>{{ attribute_type.id }}</td>
                        <td>{{ attribute_type.name }}</td>
                        <!-- Here should be the choice parsed to string name{{ choices[attribute_type.choice] }}-->
                        <td><a class="btn btn-sm btn-default"
                            href="{% url 'modify_attribute_type' id_attribute_type=attribute_type.id %}"><span class="#"></span>
                                Modificar</a></td>
                        <td><a class="btn btn-sm btn-default"
                            href="{% url 'delete_attribute_type' id_attribute_type=attribute_type.id %}"><span
                                class="glyphicon glyphicon-trash"></span> Eliminar</a></td>
                        <td><a class="btn btn-sm btn-default"
                            href="{% url 'visualize_attribute_type' id_attribute_type=attribute_type.id %}"><span
                                class="glyphicon glyphicon-search"></span> Visualizar</a></td>
                    </tr>
                    {% endfor %}
                </tbody>
            </table>
        </div>
        {% else %}
        <h3>No existen Tipo de Atributo</h3>
        {% endif %}
    </div>
</div>
{% endblock %}
{%extends“index.html”%}{%block title%}SGC-Administración de Tipo
德阿特里布托{%endblock%}{%block content%}
阿特里布托斯酒店管理局


{%if属性_类型%} 身份证件 阿特里布托斯酒店 {attribute_types%} {{attribute_type.id} {{attribute_type.name} {%endfor%} {%else%} 不存在心房颤动 {%endif%} {%endblock%}

当我需要传递一个特定模型的列表时,主要的问题就开始了。如何将
attribute\u type.choice
int值转换为其各自的字符串值

CHOICES = {
    0: "Numeric",
    1: "String",
    2: "Boolean",
    3: "Date",
}
然后,最简单的选择是在代码中进行查找,而不是在模板级别:

ctx = {'choice': CHOICES[type]}

使用变量作为模板中的键(有意)。有很多方法可以解决这个问题,但最好的方法是在您的视图中,而不是在您的模板中(如上所述)。

{code>{'choices':choices,'type':type}
这会传递完整的数据结构以及您只会使用一小部分的信息吗(知道是哪一部分,并故意传递3/4的无用数据)?稍后我会将其用于一个类型数组-你是什么意思?张贴你想解决的案例,而不仅仅是问题的一半。对于这种情况,可能还有更好的解决方案。