Registry / web-framework / django-classy-tags

django-classy-tags

JSON →
library4.1.0pypypi✓ verified 87d ago

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-tags
INSTALL
IMPORT
SIG · DJANGO-CLASSY-TAGS
D
django-classy-tags
web-frameworkpythonv4.1.0
Install
3.4s avg
Import
408ms
Disk
66MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v4.1.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.920 runs
installs and imports cleanly · install 0.0s · import 0.426s · 66.4MB
glibc
py 3.103.920 runs
installs and imports cleanly · install 3.4s · import 0.390s · 67MB
66MB installed
● package 66MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Tag
from classytags.core import Tag
Options
from classytags.core import Options
Argument
from classytags.arguments import Argument
AsTag
from classytags.helpers import AsTag
InclusionTag
from classytags.helpers import InclusionTag
template
from django import template

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.

from classytags.core import Tag from django import template register = template.Library() class HelloWorldTag(Tag): name = 'hello_world' def render_tag(self, context): return 'Hello, classy world!' register.tag(HelloWorldTag) # In your Django template (.html file): # {% load my_app_tags %} # {% hello_world %} # Output: Hello, classy world!
Debug
Known issues
breakingSupport for older Python versions was dropped in major releases. Version 3.0.0 dropped Python 3.5 and 3.6. Version 4.0.0 dropped Python 3.7, while adding support for Python 3.11.
fix
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.
affects: >=3.0.0, >=4.0.0
breakingSupport for older Django versions was removed in major releases. Version 2.0.0 dropped Django < 2.2. Version 4.0.0 dropped support for Django < 3.2, and added support for Django 4.1. Version 4.1.0 supports Django 4.2.
fix
Upgrade Django to a compatible version. For django-classy-tags 4.1.0, Django >=3.2 and up to 4.2 are supported.
affects: >=2.0.0, >=4.0.0
gotchaUnlike some other template tag libraries, django-classy-tags does not automatically register or load your tags. You must explicitly define and register each tag.
fix
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 %}`.
affects: All versions
gotchaWhen subclassing `classytags.helpers.AsTag`, you must define at least one breakpoint in the `Options` class, and the *last* breakpoint must correspond to exactly one argument (typically for the 'as varname' part).
fix
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.
affects: All versions
Errors
Common errors & fixes
TemplateSyntaxError: 'your_tag_name' is not a registered tag library. Must be one of: ...
The Django application containing the custom template tags (including those built with django-classy-tags) is either not listed in INSTALLED_APPS, the templatetags directory or its __init__.py file is missing, the Python module containing the tag is incorrectly named, or the Django development server has not been restarted after the tag's creation or modification.
fix
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.
TemplateSyntaxError: Could not parse the remainder: '(...)' from '(...)'
This error typically occurs when Django's template engine encounters syntax it doesn't understand within a tag, often due to complex Python expressions, missing quotes, incorrect spacing, or unsupported operations directly within `{% %}` or `{{ }}` blocks.
fix
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.
ModuleNotFoundError: No module named 'classytags'
The `django-classy-tags` library is not installed in your active Python environment, or there is a typo in the import statement for one of its modules (e.g., `from classytags.core import Tag`).
fix
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.
ImproperlyConfigured: AsTag subclasses require at least one breakpoint.
When creating a custom template tag by subclassing `classytags.helpers.AsTag`, the `options` attribute in your tag class is defined without including at least one 'breakpoint', typically the `'as'` keyword, which is necessary for `AsTag` to correctly parse the variable assignment.
fix
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))`.
Upgrade
Version history
4.1.0latest on PyPI · released Jul 29, 2023
Audit
Dependencies
DjangorequiredCore framework dependency. Version 4.1.0 supports Django >=3.2, 4.0, 4.1, 4.2.
PythonrequiredRequires Python >=3.8. Version 4.1.0 supports Python 3.8, 3.9, 3.10, and 3.11.
Agent activity
8 hits · last 30 days
node
8
Resources
django-classy-tags — pip install django-classy-tags · libregistry