views:

334

answers:

3

In view:

return render_to_response('template.html',
                          {'headers': list(sort_headers.headers()) },
                          context_instance=RequestContext(request))

In template:

{{ headers }}
<br />
{{ headers|slice:"1" }}

In browser:

[{'url': '?ot=desc&amp;o=0', 'text': 'Nombre', 'class_attr': ' class="sorted ascending"', 'sortable': True}, {'url': '?ot=asc&amp;o=1', 'text': 'Valor', 'class_attr': '', 'sortable': True}, {'url': '?ot=asc&amp;o=2', 'text': 'Inventario', 'class_attr': '', 'sortable': False}, {'url': '?ot=asc&amp;o=3', 'text': 'Fecha Creacion', 'class_attr': '', 'sortable': True}]

[{'url': '?ot=desc&amp;o=0', 'text': 'Nombre', 'class_attr': ' class="sorted ascending"', 'sortable': True}]

I get a list node with {{ headers|slice:"1" }}, but now, how to get a dict value? for example 'url' returns '?ot=desc&amp;o=0'.

Note: Cant use {% for %}.

A: 

I'm not sure I understand your question but to get the values form a dict in the template you can use the methods items or values.

If you use {{dict.items}} you get a list of tuples (key, value) and you can get the value simply using tuple.1.

If you use {{dict.values}} you just get a list of the values of the dictionary.

Facundo
A: 

I think you want:

{% with headers|slice:"1" as data %}
<a href="{{ data.url }}"{{ data.class_attr|safe }}>{{ data.text }}</a>
{% endwith %}
SmileyChris
+3  A: 

{{ headers.1.url }}

From http://docs.djangoproject.com/en/dev/topics/templates/#variables:

Technically, when the template system encounters a dot, it tries the following lookups, in this order:
    * Dictionary lookup
    * Attribute lookup
    * Method call
    * List-index lookup

So, instead of {{ headers|slice:"1" }} you can do {{ headers.1 }}. And then to access the url key, you just append it: {{ headers.1.url }}.

HTH.

dijxtra
Yes, It is. thx.
panchicore