Metadata-Version: 2.1
Name: django_expr
Version: 0.0.1
Summary: Django template tag and filter for evaluating Python expressions
Author-email: Juro Oravec <juraj.oravec.josefson@gmail.com>
License: MIT
Project-URL: Homepage, https://github.com/JuroOravec/alpinui/blob/main/packages/django-expr
Keywords: django,python,expression,django tag,django filter
Classifier: Framework :: Django
Classifier: Framework :: Django :: 4.2
Classifier: Framework :: Django :: 5.0
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.8
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Requires-Python: <4.0,>=3.8
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: Django>=4.2

# Django Expressions

Django template tag and filter for evaluating Python expressions.

## Demo

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

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

Save the result to a variable using `as var` at the end of the tag:
```django
{% expr "[a for a in range(n)]" as result %}
{{ result }}
```

As a filter `"<expression>" | expr`:

```django
{{ "{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>"`:

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

## Installation

1. Install dependencies

    ```sh
    pip install django_expr
    ```

2. Add `django_expr` to `INSTALLED_APPS` in your `settings.py`

    ```py
    INSTALLED_APPS = [
        "django_expr",
        ...
    ]
    ```

3. Inside templates, load the tags with `{% load expr %}`:

    ```django
    {% 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`:

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