How to access array elements in a Django template?

前端 未结 4 489
慢半拍i
慢半拍i 2020-11-28 18:27

I am getting an array arr passed to my Django template. I want to access individual elements of the array in the array (e.g. arr[0], arr[1]

4条回答
  •  温柔的废话
    2020-11-28 18:45

    when you render a request tou coctext some information: for exampel:

    return render(request, 'path to template',{'username' :username , 'email'.email})
    

    you can acces to it on template like this : for variabels :

    {% if username %}{{ username }}{% endif %}
    

    for array :

    {% if username %}{{ username.1 }}{% endif %}
    {% if username %}{{ username.2 }}{% endif %}
    

    you can also name array objects in views.py and ten use it like:

    {% if username %}{{ username.first }}{% endif %}
    

    if there is other problem i wish to help you

提交回复
热议问题