-2

I converted the python dictionary into a python object, but I can't display the values ​​of the sub-array D

Views.py

def dictToObject(request):
  dictionnaire={'A': 1, 'B': {'C': 2},'D': ['E', {'F': 3}],'G':4}
  obj=json.loads(json.dumps(dictionnaire))
  context={'obj':obj}
  return render(request,'cart/cart_detail.html',context)

cart_detail.html

  {{ obj.D[0] }}
  {{ obj.D[1].F }}

I get an error (Could not parse the remainder: '[0]' from 'obj.D[0]'), I don't know why?

Khalifa
  • 33
  • 5
  • 2
    Does this answer your question? [Accessing dictionary by key in Django template](https://stackoverflow.com/questions/19745091/accessing-dictionary-by-key-in-django-template) also [How to access array elements in a Django template?](https://stackoverflow.com/questions/1700661/how-to-access-array-elements-in-a-django-template) – Abdul Aziz Barkat Sep 21 '22 at 12:44
  • 2
    `{{ obj.D.0 }}` and `{{ obj.D.1.F }}` – Abdul Aziz Barkat Sep 21 '22 at 12:45
  • it still doesn't show up, even if I put them in a for loop – Khalifa Sep 21 '22 at 12:56

2 Answers2

-1

Inside a {% %} tag, variables aren't surrounded by {{.

-1

Hope this helps

{{ obj.D.0 }}
{{ obj.D.1.F }}

as @AbdulAzizBarkat mentioned in the comment.

ilyasbbu
  • 1,468
  • 4
  • 11
  • 22