0

In a Django application, I want to use a dictionary as elements of a result.html page:

  <tbody>
    {% for element in products%}
      <tr>
        <td>{{ element['q0']['Results'][0]['Name'] }}</td>
      </tr>
    {% endfor %}
  </tbody>

But it returns Could not parse the remainder: '['q0']['Results'][0]['Name']' from 'element['q0']['Results'][0]['Name']':

    return render(request, 'todo/result.html', {'products': top_products})
  File "C:\Python36\lib\site-packages\django\shortcuts.py", line 19, in render
    content = loader.render_to_string(template_name, context, request, using=using)
  File "C:\Python36\lib\site-packages\django\template\loader.py", line 61, in render_to_string
    template = get_template(template_name, using=using)
  File "C:\Python36\lib\site-packages\django\template\loader.py", line 15, in get_template
    return engine.get_template(template_name)
  File "C:\Python36\lib\site-packages\django\template\backends\django.py", line 34, in get_template
    return Template(self.engine.get_template(template_name), self)
  File "C:\Python36\lib\site-packages\django\template\engine.py", line 143, in get_template
    template, origin = self.find_template(template_name)
  File "C:\Python36\lib\site-packages\django\template\engine.py", line 125, in find_template
    template = loader.get_template(name, skip=skip)
  File "C:\Python36\lib\site-packages\django\template\loaders\base.py", line 30, in get_template
    contents, origin, origin.template_name, self.engine,
  File "C:\Python36\lib\site-packages\django\template\base.py", line 155, in __init__
    self.nodelist = self.compile_nodelist()
  File "C:\Python36\lib\site-packages\django\template\base.py", line 193, in compile_nodelist
    return parser.parse()
  File "C:\Python36\lib\site-packages\django\template\base.py", line 478, in parse
    raise self.error(token, e)
  File "C:\Python36\lib\site-packages\django\template\base.py", line 476, in parse
    compiled_result = compile_func(self, token)
  File "C:\Python36\lib\site-packages\django\template\defaulttags.py", line 814, in do_for
    nodelist_loop = parser.parse(('empty', 'endfor',))
  File "C:\Python36\lib\site-packages\django\template\base.py", line 449, in parse
    raise self.error(token, e)
  File "C:\Python36\lib\site-packages\django\template\base.py", line 447, in parse
    filter_expression = self.compile_filter(token.contents)
  File "C:\Python36\lib\site-packages\django\template\base.py", line 563, in compile_filter
    return FilterExpression(token, self)
  File "C:\Python36\lib\site-packages\django\template\base.py", line 663, in __init__
    "from '%s'" % (token[upto:], token))
django.template.exceptions.TemplateSyntaxError: Could not parse the remainder: '['q0']['Results'][0]['Name']' from 'element['q0']['Results'][0]['Name']'

It was sent by views.py:

def getmatch(request):
    # cosas cosas cosas para obtener top_products

    print(top_products[0])
    return render(request, 'todo/result.html', {'products': top_products})

Here is an example of a product top_products[0]:

{
  "q1": {
    "Id": "q1",
    "Limit": 20,
    "Offset": 0,
    "TotalResults": 0,
    "Locale": "fr_FR",
    "Results": [],
    "Includes": {},
    "HasE rrors": false,
    "Errors": []
  },
  "q0": {
    "Id": "q0",
    "Limit": 10,
    "Offset": 0,
    "TotalResults": 1,
    "Locale": "fr_FR",
    "Results": [
      {
        "EANs": [
          "8011003827336"
        ],
        "Description": "L’aur a divine d’une femme habillée d’une essence éblouissante et sensuelle…\nEros pour Femme est le mythe signé Versace, qui déclenche la passion débordante d’Eros au pre mier regard.\n\nMais qui séduit qui ?\nEros pour Femme est une invitation à s’abandonner au désir, en osmose avec les forces de la nature apaisée.\n\nAudacieuse, cré ative et sensuelle, comme seule peut l’être la maison Versace, cette Eau de Toilette révèle une aura radieuse et une séduction irrésistible.",
        "ImageUrl": "https://w ww.sephora.fr/dw/image/v2/BCVW_PRD/on/demandware.static/-/Sites-masterCatalog_Sephora/default/dw99b648b2/images/hi-res/SKU/SKU_5/359845_swatch.jpg?sw=250&sh=250&sm=f it",
        "Name": "Eros pour Femme - Eau de Toilette",
        "Id": "P2615007",
        "CategoryId": "parfum_719097",
        "BrandExternalId": "versace_c45bfd",
        "Brand": {
          "Id": "versace_c45b fd",
          "Name": "VERSACE"
        },
        "Active": true,
        "ProductPageUrl": "https://www.sephora.fr/p/eros-pour-femme---eau-de-toilette-359845.html",
        "Disabled": false,
        "ISBNs": [],
        "FamilyIds": [],
        "UPCs": [],
        "StoryIds": [],
        "ModelNumbers": [],
        "Attributes": {},
        "QuestionIds": [],
        "AttributesOrder": [],
        "ReviewIds": [],
        "ManufacturerPartNumber s": [],
        "QAStatistics": {
          "QuestionHelpfulVoteCount": 0,
          "FirstAnswerTime": "None",
          "LastQuestionAnswerTime": "None",
          "FirstQuestionTime": "None",
          "FeaturedAnswerCount": 0,
          "LastAnswerTime": "None",
          "TagDistribution": {},
          "ContextDataDistribution": {},
          "TotalAnswerCount": 0,
          "FeaturedQuestionCount": 0,
          "LastQuestionTime": "None",
          "Question NotHelpfulVoteCount": 0,
          "BestAnswerCount": 0,
          "TagDistributionOrder": [],
          "AnswerHelpfulVoteCount": 0,
          "HelpfulVoteCount": 0,
          "AnswerNotHelpfulVoteCount": 0,
          "Total QuestionCount": 0,
          "ContextDataDistributionOrder": []
        },
        "TotalQuestionCount": 0,
        "TotalAnswerCount": 0,
        "ReviewStatistics": {
          "ContextDataDistributionOrder": [
            "Gender ",
            "Age",
            "Eyes",
            "Skin",
            "loyalty"
          ],
          "ContextDataDistribution": {
            "Gender": {
              "Id": "Gender",
              "Values": [
                {
                  "Count": 7,
                  "Value": "Female"
                }
              ]
            },
            "Age": {
              "Id": "Age",
              "Valu es": [
                {
                  "Count": 1,
                  "Value": "13to17"
                },
                {
                  "Count": 2,
                  "Value": "18to24"
                },
                {
                  "Count": 1,
                  "Value": "25to34"
                },
                {
                  "Count": 1,
                  "Value": "35to44"
                },
                {
                  "Count": 1,
                  "Value": "45to 54"
                },
                {
                  "Count": 1,
                  "Value": "plus54"
                }
              ]
            },
            "Eyes": {
              "Id": "Eyes",
              "Values": [
                {
                  "Count": 2,
                  "Value": "Marrons"
                },
                {
                  "Count": 3,
                  "Value": "Bleus"
                },
                {
                  "Count": 1,
                  "Value": "N oirs"
                }
              ]
            },
            "Skin": {
              "Id": "Skin",
              "Values": [
                {
                  "Count": 1,
                  "Value": "Normale"
                },
                {
                  "Count": 2,
                  "Value": "Seche"
                },
                {
                  "Count": 2,
                  "Value": "Mixte"
                },
                {
                  "Count": 1,
                  "Value": " Deshydratee"
                }
              ]
            },
            "loyalty": {
              "Id": "loyalty",
              "Values": [
                {
                  "Count": 2,
                  "Value": "Yes--Im-a-VIB"
                },
                {
                  "Count": 2,
                  "Value": "Yes--Im-a-VIB-Rouge"
                },
                {
                  "Count": 2,
                  "Value": "No"
                }
              ]
            }
          },
          "AverageOverallRating": 4.428571428571429,
          "NotHelpfulVoteCount": 1,
          "FeaturedReviewCount": 0,
          "NotRecommendedCount": 1,
          "HelpfulVoteCount": 19,
          "RatingDis tribution": [
            {
              "RatingValue": 5,
              "Count": 5
            },
            {
              "RatingValue": 2,
              "Count": 1
            },
            {
              "RatingValue": 4,
              "Count": 1
            }
          ],
          "RecommendedCount": 5,
          "RatingsOnlyReviewCount": 0,
          "To talReviewCount": 7,
          "FirstSubmissionTime": "2017-05-28T22:46:00.000+00:00",
          "LastSubmissionTime": "2020-03-21T19:01:26.000+00:00",
          "SecondaryRatingsAveragesOrder": [],
          "SecondaryRatingsAverages": {},
          "OverallRatingRange": 5,
          "TagDistributionOrder": [],
          "TagDistribution": {}
        },
        "TotalReviewCount": 7,
        "FilteredQAStatistics": {
          "Ques tionHelpfulVoteCount": 0,
          "FirstAnswerTime": "None",
          "LastQuestionAnswerTime": "None",
          "FirstQuestionTime": "None",
          "FeaturedAnswerCount": 0,
          "LastAnswerTime": "None",
          "TagD istribution": {},
          "ContextDataDistribution": {},
          "TotalAnswerCount": 0,
          "FeaturedQuestionCount": 0,
          "LastQuestionTime": "None",
          "QuestionNotHelpfulVoteCount": 0,
          "Best AnswerCount": 0,
          "TagDistributionOrder": [],
          "AnswerHelpfulVoteCount": 0,
          "HelpfulVoteCount": 0,
          "AnswerNotHelpfulVoteCount": 0,
          "TotalQuestionCount": 0,
          "ContextDat aDistributionOrder": []
        },
        "FilteredReviewStatistics": {
          "ContextDataDistributionOrder": [
            "Gender",
            "Age",
            "Eyes",
            "Skin",
            "loyalty"
          ],
          "ContextDataDistribution": {
            "Gen der": {
              "Id": "Gender",
              "Values": [
                {
                  "Count": 7,
                  "Value": "Female"
                }
              ]
            },
            "Age": {
              "Id": "Age",
              "Values": [
                {
                  "Count": 1,
                  "Value": "13to17"
                },
                {
                  "Count": 2,
                  "Value": "18to24"
                },
                {
                  "Count": 1,
                  "Value": "25to34"
                },
                {
                  "Count": 1,
                  "Value": "35to44"
                },
                {
                  "Count": 1,
                  "Value": "45to54"
                },
                {
                  "Count": 1,
                  "Value": "plus54"
                }
              ]
            },
            "Eyes": {
              "Id": "Eyes",
              "Value s": [
                {
                  "Count": 2,
                  "Value": "Marrons"
                },
                {
                  "Count": 3,
                  "Value": "Bleus"
                },
                {
                  "Count": 1,
                  "Value": "Noirs"
                }
              ]
            },
            "Skin": {
              "Id": "Skin",
              "Values": [
                {
                  "Count": 1,
                  "Value": "Nor male"
                },
                {
                  "Count": 2,
                  "Value": "Seche"
                },
                {
                  "Count": 2,
                  "Value": "Mixte"
                },
                {
                  "Count": 1,
                  "Value": "Deshydratee"
                }
              ]
            },
            "loyalty": {
              "Id": "loyalty",
              "Values": [
                {
                  "Count": 2,
                  "Value": "Yes--Im-a-VIB"
                },
                {
                  "Count": 2,
                  "Value": "Yes--Im-a-VIB-Rouge"
                },
                {
                  "Count": 2,
                  "Value": "No"
                }
              ]
            }
          },
          "AverageOverallRating": 4.428571428571429,
          "NotHelpfulVoteCo unt": 1,
          "FeaturedReviewCount": 0,
          "NotRecommendedCount": 1,
          "HelpfulVoteCount": 19,
          "RatingDistribution": [
            {
              "RatingValue": 5,
              "Count": 5
            },
            {
              "RatingValue": 2,
              "Count ": 1
            },
            {
              "RatingValue": 4,
              "Count": 1
            }
          ],
          "RecommendedCount": 5,
          "RatingsOnlyReviewCount": 0,
          "TotalReviewCount": 7,
          "FirstSubmissionTime": "2017-05-28T22:46:00.000+00 :00",
          "LastSubmissionTime": "2020-03-21T19:01:26.000+00:00",
          "SecondaryRatingsAveragesOrder": [],
          "SecondaryRatingsAverages": {},
          "OverallRatingRange": 5,
          "TagDistri butionOrder": [],
          "TagDistribution": {}
        }
      }
    ],
    "Includes": {},
    "HasErrors": false,
    "Errors": []
  },
  "d": {
    "attributs": {
      "Doux": {
        "claimed_benefit": 0,
        "perceived_benefit": 0
      },
      "Délicat": {
        "claimed_benefit": 0,
        "perceived_benefit": 0
      },
      "Elegant": {
        "claimed_benefit": 0,
        "perceived_benefit": 0
      },
      "Mature": {
        "claimed_benefit": 0,
        " perceived_benefit": 0
      },
      "Sexy": {
        "claimed_benefit": 0,
        "perceived_benefit": 0
      },
      "Féminin": {
        "claimed_benefit": 0,
        "perceived_benefit": 0
      },
      "Frais": {
        "claimed_ benefit": 0,
        "perceived_benefit": 0.14285714285714285
      },
      "Classe": {
        "claimed_benefit": 0,
        "perceived_benefit": 0
      },
      "Mou": {
        "claimed_benefit": 0,
        "perceived_benefit": 0.14285714285714285
      },
      "Décontracté": {
        "claimed_benefit": 0,
        "perceived_benefit": 0
      },
      "Comme les autres": {
        "claimed_benefit": 0,
        "perceived_benefit": 0
      },
      "Jeu ne femme": {
        "claimed_benefit": 1,
        "perceived_benefit": 0.14285714285714285
      },
      "charmant": {
        "claimed_benefit": 0,
        "perceived_benefit": 0
      },
      "Gai": {
        "claimed_benefit": 0,
        "perceived_benefit": 0
      },
      "Propre": {
        "claimed_benefit": 0,
        "perceived_benefit": 0
      },
      "Eté": {
        "claimed_benefit": 0,
        "perceived_benefit": 0
      },
      "Rafraîchissant ": {
        "claimed_benefit": 0,
        "perceived_benefit": 0
      },
      "Chaud": {
        "claimed_benefit": 0,
        "perceived_benefit": 0
      },
      "Masculin": {
        "claimed_benefit": 0,
        "perceived_benefit ": 0
      },
      "Fiable": {
        "claimed_benefit": 0,
        "perceived_benefit": 0
      },
      "Mystérieux": {
        "claimed_benefit": 0,
        "perceived_benefit": 0
      },
      "Furtif": {
        "claimed_benefit": 0,
        "perceived_benefit": 0.14285714285714285
      },
      "Fort": {
        "claimed_benefit": 0,
        "perceived_benefit": 0.14285714285714285
      },
      "Hivernal": {
        "claimed_benefit": 0,
        "perceived_ benefit": 0
      },
      "Herbacé": {
        "claimed_benefit": 0,
        "perceived_benefit": 0
      },
      "Plantes": {
        "claimed_benefit": 0,
        "perceived_benefit": 0
      },
      "Big brands": {
        "claimed_be nefit": 0,
        "perceived_benefit": 0
      },
      "Luxueux": {
        "claimed_benefit": 0,
        "perceived_benefit": 0
      },
      "Connu": {
        "claimed_benefit": 0,
        "perceived_benefit": 0.2857142857142857
      },
      "A la mode": {
        "claimed_benefit": 0,
        "perceived_benefit": 0
      }
    }
  },
  "total": 0
}
Revolucion for Monica
  • 2,848
  • 8
  • 39
  • 78
  • Does this answer your question? [Accessing dictionary by key in Django template](https://stackoverflow.com/questions/19745091/accessing-dictionary-by-key-in-django-template) – iklinac Feb 12 '21 at 15:38
  • https://stackoverflow.com/questions/1275735/how-to-access-a-dictionary-element-in-a-django-template .. May be this will work "element.q0.Results[0].Name" – Amit Feb 12 '21 at 15:38
  • 1
    @Amit Results.0.Name – iklinac Feb 12 '21 at 15:40
  • @iklinac .. Thank you .. Yes it should be that since ".0" will be evaluated as list index finally in the order of look up for the context – Amit Feb 12 '21 at 15:44
  • Yes, that was that. Thanks for your help ! – Revolucion for Monica Feb 12 '21 at 15:46

1 Answers1

0

Instead of using square bracket notations, Django Template Language uses dots. So the result should be: {{ element.q0.Results.0.Name }}

pe.kne
  • 655
  • 3
  • 16