Ninja Turtle
Ninja Turtle

Reputation: 156

Access tuple through index value in django template

How do I access the value of tuple through indexing in a template? When i try to do this....django gives me the error : Could not parse the remainder: '[0]' from 'tuple[0]'.

template.html:

{{ tuple[0] }}

views.py:

def fun(request):
    tuple=('a','b','c','d')
    return render(request,'template.html',{'tuple':tuple})

Upvotes: 5

Views: 4907

Answers (1)

Andriy Ivaneyko
Andriy Ivaneyko

Reputation: 22041

Simply access tuple as in code below:

{{ tuple.0 }}

Also consider using django built-in template tags to iterate over your data, see simple usage below:

{% for item in tuple %}
    <span>
      {{ item }}
    </spam>
{% endfor %}

Upvotes: 10

Related Questions