Skip to main content

Django Expressions

Django template tag and filter for evaluating Python expressions.

Demo

As a template tag {% expr "<expression>" %}

{% expr "[a for a in range(n)]" %}

Save the result to a variable using as var at the end of the tag:

{% expr "[a for a in range(n)]" as result %}
{{ result }}

As a filter "<expression>" | expr:

{{ "{k: v.name for k, v in employees_dict.values() }" | expr }}

To chain an expression after another filter, define a lambda expression as the filter argument value | expr:"lambda x: <expression>":

{{ employees_list | first | expr:"lambda x: x.first_name + ' ' + x.last_name" }}

Installation

  1. Install dependencies

    pip install django_expr
    
  2. Add django_expr to INSTALLED_APPS in your settings.py

    INSTALLED_APPS = [
        "django_expr",
        ...
    ]
    
  3. Inside templates, load the tags with {% load expr %}:

    {% load expr %}
    {% expr "[{k: not v} for k, v in ({'a': 'b', 'c': 0}).items()]" %}
    
  4. If you want to automatically include the expr tag and filter, add the tags as a 'builtin' in settings.py:

TEMPLATES = [
    {
        ...,
        'OPTIONS': {
            'context_processors': [
                ...
            ],
            'builtins': [
                'django_expr.templatetags.expr',
            ]
        },
    },
]

Metadata

Release files for django-expr 0.1.0

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-expr 0.1.0
File Size Uploaded
django_expr-0.1.0.tar.gz 7.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-expr 0.1.0
File Interpreter ABI Platform
django_expr-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 15.3 kB

Release files / django_expr-0.1.0.tar.gz

Download URL django_expr-0.1.0.tar.gz
Size 7.9 kB
Tags Source
SHA-256 checksum
How to use checksums
c6800e83edfa6afb420e59e1cda84a051a493a5ed511da25e6b48b7dc8a1bd55
BLAKE2b-256 checksum
How to use checksums
56fd369d9cd58bba3f51d7cabb544bb05ec6d1a56460d472ab3f685ea94eb50c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.11.5

Release files / django_expr-0.1.0-py3-none-any.whl

Download URL django_expr-0.1.0-py3-none-any.whl
Size 7.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
577eccdc7da9f69469d35e54e6c03244b926d2f6caa012ce5d4313b1b0ffb676
BLAKE2b-256 checksum
How to use checksums
b9038950d147bf245c1e62bb82c5c274c7c557de2f47b63ee4b97fa019a6369b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.11.5

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

0.0.1

2 release files

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