Django forloop template variables

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ananth21
    New Member
    • Oct 2011
    • 13

    Django forloop template variables

    i am trying to learn django, after starting off with python for sometime as a beginner.
    after comfortably going through django book for the initial
    few topics iam stuck at trying to understand template variables-like (forloop.counte r,forloop.revco unter, etc)
    kindly help.
    note that iam a beginner.
    thank you
  • Varsha1285
    New Member
    • Feb 2023
    • 16

    #2
    Ok so as you are aware of python then For loop functionality i.e., iterating over sequences like of array ,dictionary and list etc. same for Django.
    What differs is Syntax-
    List->
    suppose you have list named cars and elements are present in it like Suzuki,Tesla,Ma hindra.
    {% for x in cars %}
    <h1>{{ x }}</h1>
    {% endfor %}

    this for loop will iterate through the elements of list cars and will return the elements .
    Dictionary->
    {% for x in cars %}
    <h1>{{ x.brand }}</h1>
    <p>{{ x.model }}</p>
    <p>{{ x.year }}</p>
    {% endfor %}

    here Output will be-
    Suzuki
    Sedans
    2003
    Tesla
    Model x
    2010,
    Mahindra
    Thar
    2006

    Comment

    Working...