django-expr 0.1.0

Creator: codyrutscher

Last updated:

Add to Cart

Description:

djangoexpr 0.1.0

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 to INSTALLED_APPS in your settings.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' in settings.py:


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

License

For personal and professional use. You cannot resell or redistribute these repositories in their original state.

Customer Reviews

There are no reviews.