Django template tag and filter for evaluating Python expressions
Project description
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
-
Install dependencies
pip install django_expr
-
Add
django_exprtoINSTALLED_APPSin yoursettings.pyINSTALLED_APPS = [ "django_expr", ... ]
-
Inside templates, load the tags with
{% load expr %}:{% load expr %} {% expr "[{k: not v} for k, v in ({'a': 'b', 'c': 0}).items()]" %}
-
If you want to automatically include the
exprtag and filter, add the tags as a 'builtin' insettings.py:
TEMPLATES = [
{
...,
'OPTIONS': {
'context_processors': [
...
],
'builtins': [
'django_expr.templatetags.expr',
]
},
},
]
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file django_expr-0.1.0.tar.gz.
File metadata
- Download URL: django_expr-0.1.0.tar.gz
- Upload date:
- Size: 7.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.11.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c6800e83edfa6afb420e59e1cda84a051a493a5ed511da25e6b48b7dc8a1bd55
|
|
| MD5 |
aeb54da02c52b029b4e15442d3ac2d19
|
|
| BLAKE2b-256 |
56fd369d9cd58bba3f51d7cabb544bb05ec6d1a56460d472ab3f685ea94eb50c
|
File details
Details for the file django_expr-0.1.0-py3-none-any.whl.
File metadata
- Download URL: django_expr-0.1.0-py3-none-any.whl
- Upload date:
- Size: 7.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.11.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
577eccdc7da9f69469d35e54e6c03244b926d2f6caa012ce5d4313b1b0ffb676
|
|
| MD5 |
011f9c2639ed0dd6b38167ca844b67b5
|
|
| BLAKE2b-256 |
b9038950d147bf245c1e62bb82c5c274c7c557de2f47b63ee4b97fa019a6369b
|