I set up my Django project to use MySQL, so that I could use a list in a JSONField. Little did I know that accessing elements of that list by their index would be another problem entirely. There are two confounding things. First, in Jinja2, you can access a for-loop index with the loop.index
or loop.index0
keywords, but in a Django project, you need to use forloop.counter
or forloop.counter0`. The trailing zero on those keywords specify a zero-indexed counter rather than a one-indexed counter. The second thing is that you need to provide a custom template tag in order to cleanly access elements in a JSONField list by their index.
Tag Archives: MySQL
Set Up MySQL for a Django Application
I wanted to store a variable list of things in a Django Model. One way to do that is to create another Model, another way is to use a JSON field in your current Django Model. Unfortunately, the default SQLite3 database does not support JSON fields, and you will need to set up a MySQL database.