length Filter

Number Most Useful

Documentation

Returns the length of a value, which can be a string or a sequence.

Variable

classes = {
    'Python': [
        'Intro Python', 'Advanced Python', 'Data Science', 'Django'
    ],
    'Databases': [
        'Intro PostgreSQL', 'Intro MySQL', 'Intro SQL Server', 'Intro Oracle'
    ],
    'Web': [
        'HTML', 'CSS', 'JavaScript'
    ],
    'XML': [
        'Intro XML'
    ]
}

Template

<ol>
  {% for category, titles in classes.items %}
    <li>
      {{ category }}: {{ titles|length }}
      class{{ titles|pluralize:"es" }}
    </li>
  {% endfor %}
</ol>

Result

<ol>
    <li>Python: 4 classes</li>
    <li>Databases: 4 classes</li>
    <li>Web: 3 classes</li>
    <li>XML: 1 class</li>
</ol>

Did we get something wrong? Is there a use case for the length filter that we should add? Please let us know.

Send Feedback

Official Documentation
This page last updated on April 14, 2020, 9:48 a.m. EST