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_expr
toINSTALLED_APPS
in yoursettings.py
INSTALLED_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
expr
tag 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
Hashes for django_expr-0.0.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 141988f894b96e9af56dc9204870070af6212e0cbeb51bec6e2326a0124677e9 |
|
MD5 | e64c38e50bc2b6fe248f16865b878810 |
|
BLAKE2b-256 | d8784f07687f5a964019a70490193cfa4c8dd2078d7ab1eada3ceec8c3c33453 |