1👍
✅
Best approach:
def roli(roles, i):
"""Return the rol[i]"""
return roles[i]
In Jinja:
{{ roles|roli:i }}
I would recomend you to read the documentation about tags
: https://docs.djangoproject.com/en/4.1/howto/custom-template-tags/ you need some extra steps to register the template tag.
Other approach.
A workaround could be the following:
def index(request):
roles = ['admin', 'teacher', 'student']
num=1
return render(request=request, template_name='show_all.html', context={'roles': roles, 'num': num, 'rolesi': roles[num]})
And then:
show_all.html
{{rolesi}}
Source:stackexchange.com