Skip to main content

django-multiforloop provides an enhancement to django's builtin forloop template tag, which makes it easier to iterate over multiple lists simultaneously (acting similarly to Python's zip).

multiforloop allows this Python idiom to be used in django templates:

for x,y in zip(x_list, y_list):
	print x,y

Normally, to iterate over multiple lists simultaneously in django templates, the lists must be zipped in the view and passed in as an extra context variable. When this is the only extra processing needed in a view (eg. with generic views), this can result in a fair bit of unneeded boilerplate code.

Usage

Rendering this template

    {% load multifor %}
    {% for x in x_list; y in y_list %}
      {{ x }}:{{ y }}
    {% endfor %}

with this context

    context = {
        "x_list": ('one', 1, 'carrot'),
        "y_list": ('two', 2, 'orange')
    }

will output

    one:two
    1:2
    carrot:orange

The multifor tag library also includes a for_longest tag that functions similarly to izip_longest from Python's itertools library. Whereas the normal for loop will truncate all inputs to the length of the shortest, for_longest will iterate over all values of the longest, filling any shorter inputs with the value in settings.TEMPLATE_STRING_IF_INVALID ('' by default).

Observe the difference:

Rendering this template

    {% load multifor %}
    {% for x in x_list; y in y_list %}
      {{ x }}:{{ y }}
    {% endfor %}

with this context

    context = {
        "x_list": ('one', 1, 'carrot'),
        "y_list": ('two', 2)
    }

will output

    one:two
    1:2

While rendering this template

    {% load multifor %}
    {% for_longest x in x_list; y in y_list %}
      {{ x }}:{{ y }}
    {% endfor %}

with the same context

    context = {
        "x_list": ('one', 1, 'carrot'),
        "y_list": ('two', 2)
    }

will output

    one:two
    1:2
    carrot:

Installation

  1. pip install django-multiforloop
  2. Include 'multiforloop' in your settings.py's list of installed apps
  3. Add {% load multifor %} to the top of any templates which use the multiforloop

Metadata

Release files for django-multiforloop 0.3.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-multiforloop 0.3.1
File Size Uploaded Provenance Actions
django-multiforloop-0.3.1.tar.gz 4.3 kB Without provenance Details for django-multiforloop-0.3.1.tar.gz

Release files / django-multiforloop-0.3.1.tar.gz

Download URL django-multiforloop-0.3.1.tar.gz
Size 4.3 kB
Tags Source
SHA-256 checksum
How to use checksums
a9e4f200a0900ad1fdacc8f6496cc4f92cd5ff62ef5359af404cf120b96cd347
BLAKE2b-256 checksum
How to use checksums
9b4412c5f7c07de19b239f6ec1f25e2aa53f5a970c3a18d97edc09c2895070db
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.15.0 pkginfo/1.7.0 requests/2.25.1 setuptools/44.1.1 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/2.7.15+

Security for django-multiforloop 0.3.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page