django-classy-tags is a Python library that provides a class-based approach to writing custom template tags for Django. It aims to make template tag creation easier, shorter, and more fun by offering an extensible argument parser, significantly reducing boilerplate code while remaining fully compatible with the existing Django templating infrastructure. It is currently at version 4.1.0 and appears to have an active release cadence, with recent updates for Django and Python compatibility.
pip install django-classy-tagsVerified import paths — ran on the pinned version, not inferred.
To create a simple tag, subclass `classytags.core.Tag` and implement the `render_tag` method, which receives the Django context and any defined tag arguments. Finally, register your tag class with a `django.template.Library` instance.
Ensure your project runs on Python >=3.8. For version 4.1.0, Python >=3.8 is required, with optimal support for Python 3.11.
Upgrade Django to a compatible version. For django-classy-tags 4.1.0, Django >=3.2 and up to 4.2 are supported.
Always use `register = template.Library()` and `register.tag(YourTagClass)` in your `templatetags/*.py` file, and load your custom tags in templates using `{% load your_app_tags %}`.Ensure `options = Options(Argument('some_arg'), 'as', Argument('varname', required=False, resolve=False))` pattern is followed for `AsTag` subclasses, or similar structures for other arguments.Ensure the Django app is added to `INSTALLED_APPS` in `settings.py`, verify that a `templatetags` directory exists within your app and contains an `__init__.py` file, confirm the Python module (`.py` file) inside `templatetags` is correctly named (e.g., `my_tags.py` for `{% load my_tags %}`), and restart your Django development server after making changes.Simplify complex expressions by performing calculations or logic in your Django views or within a custom template filter/tag. Ensure all arguments within your `django-classy-tags` tags are correctly quoted and spaced according to Django's template language rules, avoiding direct method calls or subscripting within `{{ }}` where not explicitly supported.Install the library using pip: `pip install django-classy-tags`. Double-check all `import` statements in your code that reference `classytags` for any spelling errors.
When defining `options` for an `AsTag` subclass, ensure you include the `'as'` breakpoint and the `Argument` for the variable name, for example: `options = Options(Argument('my_arg'), 'as', Argument('varname', required=False, resolve=False))`.