Reputation: 99
Imagine you have a dictionary like this in a view
CHOICES = (
(0, "Numeric"),
(1, "String"),
(2, "Boolean"),
(3, "Date"),
)
Later in the view I pass my dictionary as a context to the template. Like this:
EDIT 1:
# type is a int
type = 2
ctx = {'choices':CHOICES, 'type':dict(type)}
The problem is that I can't parse my dictionary in HTML. In this example, I want to return the string "Boolean"
Here's my snippet:
{{ choices[type] }}
How can I solve this?
I think there's a solution using for
cycles and then compare the variable with the type, but that's too much code. Or I can pass the string instead of the whole dictionary, but that's not what I want. Later I'll use it with an array of types so the last solution can't be used.
Edit 2:
Ok. To make it simple...
In 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
Imagine I already filled the db with some random values.
In 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))
In my list_attribute_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 %}
The main problem starts when I need to pass a list of a certain model like this one. Any ideas how to convert attribute_type.choice
int value to his respective string value?
Upvotes: 1
Views: 98
Reputation: 89007
CHOICES
is not a dictionary - just use a dictionary literal:
CHOICES = {
0: "Numeric",
1: "String",
2: "Boolean",
3: "Date",
}
Then the easiest option is to do the lookup in your code, not at a template level:
ctx = {'choice': CHOICES[type]}
Using a variable as a key in a template isn't supported in django (intentionally). There are ways around this, but the bestway is to do it in your view, not in your template (as above).
Upvotes: 1