154

This may be simple, but I looked around and couldn't find an answer. What's the best way to reference a single item in a list from a Django template?

In other words, how do I do the equivalent of {{ data[0] }} within the template language?

5 Answers 5

265

It looks like {{ data.0 }}. See Variables and lookups.

3
  • 66
    The annoying thing is that I can't say {{ data.foo }}, where foo is a variable with an index value in it and not a property name. Jan 10, 2011 at 23:06
  • 1
    If you're willing to create a custom tag, you can do a lot more. Here we're just working with the built-in stuff. Apr 16, 2015 at 13:55
  • Link not working anymore, try this one : docs.djangoproject.com/en/1.10/ref/templates/api/…
    – Speccy
    Mar 19, 2017 at 15:00
122

A better way: custom template filter: https://docs.djangoproject.com/en/dev/howto/custom-template-tags/

such as get my_list[x] in templates:

in template

{% load index %}
{{ my_list|index:x }}

templatetags/index.py

from django import template
register = template.Library()

@register.filter
def index(indexable, i):
    return indexable[i]

if my_list = [['a','b','c'], ['d','e','f']], you can use {{ my_list|index:x|index:y }} in template to get my_list[x][y]

It works fine with "for"

{{ my_list|index:forloop.counter0 }}

Tested and works well ^_^

9
  • 2
    One of the simplest explanations to learn Template Tags' application!
    – vanguard69
    Jul 14, 2015 at 21:32
  • 7
    This was great! But with the {{ List|index:x }} format, how do I access values where I would normally use a dot? {{ (List|index:x).name }} obviously does not work. Thank you!
    – JTFouquier
    Jun 1, 2016 at 17:54
  • Exactly what I was looking for. Thank you! Jul 9, 2017 at 11:38
  • 1
    @SuperCode '0' means getting the index which starts from 0, not 1
    – WeizhongTu
    Sep 16, 2020 at 6:14
  • 1
    also, as @JTFouquier asked, you can access element attributes using with as explained here
    – jerome
    Apr 30, 2021 at 8:35
26

{{ data.0 }} should work.

Let's say you wrote data.obj django tries data.obj and data.obj(). If they don't work it tries data["obj"]. In your case data[0] can be written as {{ data.0 }}. But I recommend you to pull data[0] in the view and send it as separate variable.

2

@jennifer06262016, you can definitely add another filter to return the objects inside a django Queryset.

@register.filter 
def get_item(Queryset):
    return Queryset.your_item_key

In that case, you would type something like this {{ Queryset|index:x|get_item }} into your template to access some dictionary object. It works for me.

0

first make sure that the app contain custom tag function is added to INSTALLED APP follow the instruction below

1 in the main directory app create another directory with name templatetags at the same level as model.py,views.py,urls.py.

2 inside of templatetags directory create init.py and another file with the name of your custom tags, for example: my_tags.py.

MainApp/
    __init__.py
    models.py
    templatetags/
        __init__.py
        my_tags.py
    views.py

in your template you would use the following:

{% load my_tags.py %}

in my_tags.py:

from django import template 
register = template.Library()
@register.filter(name="get")
def get(indexable, i):
    return indexable[i]

in views.py:

number = [0,1,2,3,4,5]

return render(
    "index.html", {"number":number}

)

in index.html:

{% load my_tags %}

{{numer|get:forloop.counter0}}

reference How to create custom template tags and filters

i hope you understand, because ingles not my native language

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.