[Django] #32526: Class based custom template filters seem to be broken since Django 2.0

49 views
Skip to first unread message

Django

unread,
Mar 8, 2021, 5:58:09 PM3/8/21
to django-...@googlegroups.com
#32526: Class based custom template filters seem to be broken since Django 2.0
-------------------------------------------+------------------------
Reporter: 884756834 | Owner: nobody
Type: Bug | Status: new
Component: Template system | Version: 3.1
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------------+------------------------
In Django 1.10, writing a custom template filter using a class used to
work:

{{{
#!div style="font-size: 80%"
Code highlighting:
{{{#!python
class MyCustomFilter(object):
__name__ = 'custom_filter'

def __call__(self, value):
if not isinstance(value, str):
return value

return MyHelperClass(value).do_stuff()

def custom_filter(value) = 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:

{{{
#!div style="font-size: 80%"
Code highlighting:
{{{#!python
def custom_filter(value):
if not isinstance(value, str):
return value

return MyHelperClass(value).do_stuff()
}}}
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/32526>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Mar 8, 2021, 6:03:11 PM3/8/21
to django-...@googlegroups.com
#32526: Class based custom template filters seem to be broken since Django 2.0
---------------------------------+--------------------------------------

Reporter: 884756834 | Owner: nobody
Type: Bug | Status: new
Component: Template system | Version: 3.1
Severity: Normal | Resolution:

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
---------------------------------+--------------------------------------
Description changed by 884756834:

Old description:

New description:

In Django 1.10, writing a custom template filter using a class used to
work:

{{{
#!div style="font-size: 80%"
Code highlighting:
{{{#!python
class MyCustomFilter(object):
__name__ = 'custom_filter'

def __call__(self, value):
if not isinstance(value, str):
return value

return MyHelperClass(value).do_stuff()

def 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:

{{{
#!div style="font-size: 80%"
Code highlighting:
{{{#!python
def custom_filter(value):
if not isinstance(value, str):
return value

return MyHelperClass(value).do_stuff()
}}}
}}}

--

--
Ticket URL: <https://code.djangoproject.com/ticket/32526#comment:1>

Django

unread,
Mar 8, 2021, 6:06:30 PM3/8/21
to django-...@googlegroups.com
#32526: Class based custom template filters seem to be broken since Django 2.0
---------------------------------+--------------------------------------

Reporter: 884756834 | Owner: nobody
Type: Bug | Status: new
Component: Template system | Version: 3.1
Severity: Normal | Resolution:

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
---------------------------------+--------------------------------------
Description changed by 884756834:

Old description:

> In Django 1.10, writing a custom template filter using a class used to


> work:
>
> {{{
> #!div style="font-size: 80%"
> Code highlighting:
> {{{#!python
> class MyCustomFilter(object):
> __name__ = 'custom_filter'
>
> def __call__(self, value):
> if not isinstance(value, str):
> return value
>
> return MyHelperClass(value).do_stuff()
>

> def 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:
>
> {{{
> #!div style="font-size: 80%"
> Code highlighting:
> {{{#!python
> def custom_filter(value):
> if not isinstance(value, str):
> return value
>
> return MyHelperClass(value).do_stuff()
> }}}
> }}}

New description:

In Django 1.10, writing a custom template filter using a class used to
work:

{{{
#!div style="font-size: 80%"
Code highlighting:
{{{#!python
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:

{{{
#!div style="font-size: 80%"
Code highlighting:
{{{#!python
def custom_filter(value):
if not isinstance(value, str):
return value

return MyHelperClass(value).do_stuff()
}}}
}}}

--

--
Ticket URL: <https://code.djangoproject.com/ticket/32526#comment:2>

Django

unread,
Mar 9, 2021, 5:02:59 AM3/9/21
to django-...@googlegroups.com
#32526: Class based custom template filters seem to be broken since Django 2.0
---------------------------------+--------------------------------------
Reporter: 884756834 | Owner: starryrbs
Type: Bug | Status: assigned

Component: Template system | Version: 3.1
Severity: Normal | Resolution:

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
---------------------------------+--------------------------------------
Changes (by starryrbs):

* owner: nobody => starryrbs
* status: new => assigned


--
Ticket URL: <https://code.djangoproject.com/ticket/32526#comment:3>

Django

unread,
Mar 9, 2021, 6:25:13 AM3/9/21
to django-...@googlegroups.com
#32526: Class based custom template filters seem to be broken since Django 2.0
---------------------------------+--------------------------------------
Reporter: 884756834 | Owner: starryrbs
Type: Bug | Status: closed

Component: Template system | Version: 3.1
Severity: Normal | Resolution: invalid

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
---------------------------------+--------------------------------------
Changes (by Mariusz Felisiak):

* status: assigned => closed
* resolution: => invalid


Comment:

Class-based custom template filters have never been officially supported
or tested, see [https://docs.djangoproject.com/en/3.1/howto/custom-
template-tags/#writing-custom-template-filters "Writing custom template
filters" docs]:
''"Custom filters are Python functions that take one or two
arguments..."''

--
Ticket URL: <https://code.djangoproject.com/ticket/32526#comment:4>

Reply all
Reply to author
Forward
0 new messages