Macba
Macba

Reputation: 3

How to display array elements in python

I'm trying to browse this array and display it on my django project but it doesn't work.

Views.py

def(request):
  
  {"product":[
    {
      "name":"sogi",
      "desc":"solo"
    },
    {
      "name":"molo",
      "desc":"kanta"
    },
  ]
 }
   context={"tab":"product"}
   return render(request,'api/myapi.html',context)

myapi.html

  {% for pro in tab %}
    {{pro.name}}
  {% endfor %}

Upvotes: 0

Views: 85

Answers (1)

NixonSparrow
NixonSparrow

Reputation: 6378

You have to change your context creation:

def(request):
    tab = {"product": [...]}
    products = tab["products"]
    context = {"products": products}
    return render(request, "api/myapi.html", context)

And change usage in your template:

{% for product in products %}
    {% for key, value in product.items %}
        {{ key }}: {{ value }}<br>
    {% endfor %}
{% endfor %}

Upvotes: 1

Related Questions