Skip to main content

No project description provided

Project description

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-multiforloop-0.3.0.tar.gz (4.2 kB view details)

Uploaded Source

File details

Details for the file django-multiforloop-0.3.0.tar.gz.

File metadata

  • Download URL: django-multiforloop-0.3.0.tar.gz
  • Upload date:
  • Size: 4.2 kB
  • Tags: Source
  • Uploaded using 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+

File hashes

Hashes for django-multiforloop-0.3.0.tar.gz
Algorithm Hash digest
SHA256 c103d68335f1bcf4ca9491cdd18ca853cbde79af589b6d4a35aad15d8ecb1337
MD5 96a6b326e42ca75676baf7093e8d73a8
BLAKE2b-256 a63af26b6e74f7efae892628c658056f436d717b7d581f16640cbbe3bcc73bf7

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page