Opened 4 years ago
Closed 4 years ago
#32526 closed Bug (invalid)
Class based custom template filters seem to be broken since Django 2.0
Reported by: | 884756834 | Owned by: | starryrbs |
---|---|---|---|
Component: | Template system | Version: | 3.1 |
Severity: | Normal | Keywords: | |
Cc: | Triage Stage: | Unreviewed | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description (last modified by )
In Django 1.10, writing a custom template filter using a class used to work:
Code highlighting:
class MyCustomFilter(object): __name__ = 'custom_filter' def __call__(self, value): if not isinstance(value, str): return value return MyHelperClass(value).do_stuff() custom_filter = MyCustomFilter()
But in Django 2.2 (and likely from 2.0 to 3.1) this seems to raise an error:
django.template.exceptions.TemplateSyntaxError: "requires 2 arguments, 1 provided"
I believe it is caused by the changes in one of, or both, these 2 commits:
https://github.com/django/django/commit/c2a1af883e18b93a77080650feb9e959536d51ca
https://github.com/django/django/commit/620e9dd31a2146d70de740f96a8cb9a6db054fc7
As a workaround, I've been able to rewrite it:
Code highlighting:
def custom_filter(value): if not isinstance(value, str): return value return MyHelperClass(value).do_stuff()
Change History (4)
comment:1 by , 4 years ago
Description: | modified (diff) |
---|
comment:2 by , 4 years ago
Description: | modified (diff) |
---|
comment:3 by , 4 years ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
comment:4 by , 4 years ago
Resolution: | → invalid |
---|---|
Status: | assigned → closed |
Class-based custom template filters have never been officially supported or tested, see "Writing custom template filters" docs: