More standard template filters

174 views
Skip to first unread message

Collin Anderson

unread,
Apr 4, 2019, 12:06:34 PM4/4/19
to Django developers (Contributions to Django itself)
Hi All,

I use django templates a lot and I always wished there was a myvar|startswith:"teststring", myvar|endswith:"teststring" and a myvar|contains:"teststring" filter.

I instead do stuff like myvar|slice:":10" == "teststring" which is a total hack.

Is this something that could be simple and common enough to add to django as batteries-included built-in?

Thanks,
Collin

Curtis Maloney

unread,
Apr 5, 2019, 2:44:15 AM4/5/19
to django-d...@googlegroups.com
On 4/5/19 3:06 AM, Collin Anderson wrote:
> Hi All,
>
> I use django templates a lot and I always wished there was a
> myvar|startswith:"teststring", myvar|endswith:"teststring" and a
> myvar|contains:"teststring" filter.

It's almost like we need a syntax for calling methods with an argument,
just like we can with filters.

For example (bring on the sheds for bikes!)

{{ myvar.startswith<"teststring" }}

> I instead do stuff like myvar|slice:":10" == "teststring" which is a
> total hack.
>
> Is this something that could be simple and common enough to add to
> django as batteries-included built-in?

Alternatively, I recall some time in the past I may have written a
"strings" template filter library simply to provide string methods as
filters...

--
Curtis

Adam Johnson

unread,
Apr 5, 2019, 5:16:53 AM4/5/19
to django-d...@googlegroups.com
FYI Jinja2 allows (most) python expressions, so this would Just Work(TM):

{{  myvar.startswith("x") }}

Django Template Language has a lot of legacy that prevents this from happening, afaiu it would not be possible to move to it.

It's not hard to move your templates (asides from those extending external packages like the admin) to Jinja2, since the syntax is very similar and you can pop a Jinja2 backend in your TEMPLATES.



--
You received this message because you are subscribed to the Google Groups "Django developers  (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-develop...@googlegroups.com.
To post to this group, send email to django-d...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-developers.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/a2b323bc-9f60-f4c9-6074-4d103bd6f930%40tinbrain.net.
For more options, visit https://groups.google.com/d/optout.


--
Adam

Curtis Maloney

unread,
Apr 5, 2019, 5:03:40 PM4/5/19
to django-d...@googlegroups.com
On 4/5/19 8:16 PM, Adam Johnson wrote:
> FYI Jinja2 allows (most) python expressions, so this would Just Work(TM):
>
> {{  myvar.startswith("x") }}
>
> Django Template Language has a lot of legacy that prevents this from
> happening, afaiu it would not be possible to move to it.

For "legacy" read "philosophical choices" :)

--
Curtis

keshav kumar

unread,
Jan 23, 2020, 4:40:32 PM1/23/20
to Django developers (Contributions to Django itself)
There are already filters which works on advance logic. for example :- 'join'. So, is it not good to add filter for startswith, endswith and contains as well? 
To unsubscribe from this group and stop receiving emails from it, send an email to django-d...@googlegroups.com.


--
Adam

Collin Anderson

unread,
Jan 29, 2020, 12:06:09 PM1/29/20
to Django developers (Contributions to Django itself)
I think it would be helpful to have builtin startswith, endswith and contains filters.

keshav kumar

unread,
Feb 1, 2020, 11:28:03 AM2/1/20
to Django developers (Contributions to Django itself)
So, can I create as a new feature and start working on it ?

Fran Hrženjak

unread,
Feb 1, 2020, 1:07:00 PM2/1/20
to Django developers (Contributions to Django itself)
A similar ticket from 6 years ago (with some more filters) was closed as a wontfix: https://code.djangoproject.com/ticket/22162
Unless there was a change in the phylosiphy, I don’t think this will be accepted either (I don’t have an opinion about whether it shouldbe, though leaning towards not).

As a side note, I think the `in` operator fills the function of the `contains` filter:
"testing" in myvar
Reply all
Reply to author
Forward
0 new messages