Type casting template filter proposal

62 views
Skip to first unread message

h3

unread,
Oct 3, 2011, 10:50:37 AM10/3/11
to Django developers
There's numerous time where I found myself wishing for type casting
filters in templates..

For example, the following won't work because one is a string and the
other is an integer:

{% if request.GET.year == object.date.year %}
...
{% endif %}

So when I need to compare a string with an int without having to write
custom filters, I use this hack:

{% if request.GET.year == object.date.year|slugify %}
...
{% endif %}

While it works, it ain't pretty.

It would be nice if we could simply do this:

{% if request.GET.year == object.date.year|string %}
...
{% endif %}

Or this:


{% if request.GET.year|int == object.date.year %}
...
{% endif %}

Am I the only one who gets in such situations ? Is there an interest
for such filters ?

If so I might propose a patch..

Regards

Florian Apolloner

unread,
Oct 3, 2011, 12:18:18 PM10/3/11
to django-d...@googlegroups.com
Hi,

personally I am -1 on that, for the following reasons:
  • This can be done in an external library
  • You can't support every possibility, you want a string/int filter, I want a blableblub/younameit type conversion
  • playing with request.GET.* in templates is probably something you shouldn't do anyways (and other objects coming from the views should have correct types)…
Cheers,
Florian
Reply all
Reply to author
Forward
0 new messages