In einer Django-Anwendung möchte ich ein dictionnary als Elemente einer result.html
-Seite verwenden:
<tbody>
{% for element in products%}
<tr>
<td>{{ element['q0']['Results'][0]['Name'] }}</td>
</tr>
{% endfor %}
</tbody>
Aber es gibt zurück: 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']'
Es wurde gesendet von 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})
Hier ist ein Beispiel für ein Produkt 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"
}
],
"Includes": {},
"HasErrors": false,
"Errors": []
},
"d": {
"attributs": {
"Connu": {
"claimed_benefit": 0,
"perceived_benefit": 0.2857142857142857
},
"A la mode": {
"claimed_benefit": 0,
"perceived_benefit": 0
}
}
},
"total": 0
}