Django Templates First element of a List


I pass a dictionary to my Django Template,

Dictionary & Template is like this -

lists[listid] = {'name': l.listname, 'docs': l.userdocs.order_by('-id')}

{% for k, v in lists.items %}
    <ul><li>Count: {{ }}, First: {{|first }}</li></ul>
{% endfor %}

Now docs is a list of userdocs type. i.e. is an instance. So first filter returns me this instance. From this I need to extract it's id. How do I do that?

I tried {{|first }}.id and various other futile trials.

2/6/2013 9:15:11 AM

Accepted Answer

You can use the {% with %} templatetag for this sort of thing.

{% with|first as first_doc %}{{ }}{% endwith %}
11/26/2010 3:22:27 PM

You can try this:

{{ }}

Like arr.0

You can get elements by index (0, 1, 2, etc.).

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow