For Loops

for loop is used for iterating over a sequence, like looping over items in an array, a list, or a dictionary.

Example

Loop through the items of a list:

{% for x in fruits %}

  <h1>{{ x }}</h1>

{% endfor %}

Example

Loop through a list of dictionaries:

{% for x in cars %}

  <h1>{{ x.brand }}</h1>

  <p>{{ x.model }}</p>

  <p>{{ x.year }}</p>

{% endfor %}


Data From a Model

Data in a model is like a table with rows and columns.

The Member model we created earlier has five rows, and each row has three columns:

 id 

 firstname 

 lastname 

 phone 

 joined_date 

 1 

 Emil 

 Refsnes 

 5551234 

 2022-01-05 

 2 

 Tobias 

 Refsnes 

 5557777 

 2022-04-01 

 3 

 Linus 

 Refsnes 

 5554321 

 2021-12-24 

 4 

 Lene 

 Refsnes 

 5551234 

 2021-05-01 

 5 

 Stalikken 

 Refsnes 

 5559876 

 2022-09-29 

When we fetch data from the model, it comes as a QuerySet object, with a similar format as the cars example above: a list with dictionaries:

<QuerySet [

  {

    'id': 1,

    'firstname': 'Emil',

    'lastname': 'Refsnes',

    'phone': 5551234,

    'joined_date': datetime.date(2022, 1, 5)

  },

  {

    'id': 2,

    'firstname': 'Tobias',

    'lastname': 'Refsnes'

    'phone': 5557777,

    'joined_date': datetime.date(2021, 4, 1

Login
ADS CODE