I have setup a Flask app trying to fetch data from the classic Drinks API.
Here is a shortened sample of the output given by this API
{
"drinks": [
{
"idDrink": "13196",
"strDrink": "Long vodka",
"strCategory": "Ordinary Drink",
"strGlass": "Highball glass",
"strInstructions": "Shake a tall glass with ice cubes and Angostura, coating the inside of the glass. Por the vodka onto this, add 1 slice of lime and squeeze juice out of remainder, mix with tonic, stir and voila you have a Long Vodka",
"strIngredient1": "Vodka",
"strIngredient2": "Lime",
"strIngredient3": "Angostura bitters",
"strIngredient4": "Tonic water",
"strIngredient5": "Ice",
"strIngredient6": null,
"strIngredient7": null,
"strIngredient8": null,
"strIngredient9": null,
"strIngredient10": null,
"strIngredient11": null,
"strIngredient12": null,
"strIngredient13": null,
"strIngredient14": null,
"strIngredient15": null
},
{
"idDrink": "16967",
"strDrink": "Vodka Fizz",
"strCategory": "Other/Unknown",
"strGlass": "White wine glass",
"strInstructions": "Blend all ingredients, save nutmeg. Pour into large white wine glass and sprinkle nutmeg on top.",
"strIngredient1": "Vodka",
"strIngredient2": "Half-and-half",
"strIngredient3": "Limeade",
"strIngredient4": "Ice",
"strIngredient5": "Nutmeg",
"strIngredient6": null,
"strIngredient7": null,
"strIngredient8": null,
"strIngredient9": null,
"strIngredient10": null,
"strIngredient11": null,
"strIngredient12": null,
"strIngredient13": null,
"strIngredient14": null,
"strIngredient15": null
}
]
}
How do you iterate over a value that has the same name but ends with something else?
For example, they have: strIngredient1-15.
Not sure, but maybe regex is better or match/endswith and then use {{ variable }} to present it.
I understand it can be done in many ways but I can't figure out how to proceed, I just need some push in the right direction.
I was trying to loop over it but I am getting the error
NoneTypeis not iterable
On the line
{% if 'strIngredient' in inner_dict[ingr] == null %}
My full code is
<tbody>
{% for outer_dict in data_output %}
{% for inner_dict in data_output[outer_dict] %}
<tr>
<td>{{ inner_dict['idDrink'] }}</td>
<td>{{ inner_dict['strDrink'] }}</td>
<td>{{ inner_dict['strCategory'] }}</td>
<td>{{ inner_dict['strGlass'] }}</td>
<td>{{ inner_dict['strInstructions'] }}</td>
{% for ingr in inner_dict %}
{% if 'strIngredient' in inner_dict[ingr] == null %}
<td>{{ inner_dict[ingr] }}</td>
</tr>
{% endif %}
{% endfor %}
{% endfor %}
{% endfor %}
</tbody>