Accessing an array list in a Django jinja2 template
Using the dot notation in templates instead of square brackets

Search for a command to run...
Using the dot notation in templates instead of square brackets

No comments yet. Be the first to comment.
Clean your email list even before sending it to a mass emailing solution like AutoSend to avoid a high bounce rate

Try using an alternative to openclaw, which they claim is cheaper to use

Using types (and interfaces) to define a blueprint of a variable

Using GraphQL as an alternative to REST API for Hashnode

making makemigrations work in Django when migrations is not pushed to git

This may seem miniscule, but many newbies tend to forget the fact that template coding in HTML is not necessarily equivalent to language code.
In almost all programming languages (or probably all ?), we access an index of an array / list using the square brackets notation with the index number in between.
For example, in python code we would do :
fruits = ['🍎', '🍐', '🍌']
print(fruits[1]) # will print 🍐
In a Django, which uses Jinja2 templating by default, when we set the values in context and send it across to the template, we use dot followed by the index to access the array index like this :
{{ fruits.1 }}
So, if we have something like this which has the fruit and the price of it, in the context,
fruits = [['🍎', 50], ['🍐', 35], ['🍌', 22]]
context = {'fruitsWithPrice': fruits}
then we would need to use fruit.0 and fruit.1 for each fruit.
{% for fruit in fruitsWithPrice %}
{{ fruit.0 }} - ${{ fruit.1 }}<br/>
{% endfor %}
This is something I myself didn't know for a while too.