Incomplete documentation or bug? (linebreaksbr and stringfilter decorated filters)

66 views
Skip to first unread message

Carles Pina i Estany

unread,
Sep 14, 2020, 3:40:59 PM9/14/20
to django-d...@googlegroups.com

Hi,

Today I had a small surprise with templates and the linebreakbr filter.

The "problem" (or root of the confusion) is that if None (of NoneType) is given
to linebreakbr it returns a SafeString 'None' instead of None (type NoneType).

The documentation says (https://docs.djangoproject.com/en/3.1/ref/templates/builtins/#std:templatefilter-linebreaks):
"""
Replaces line breaks in plain text with appropriate HTML; a single newline
becomes an HTML line break (<br>) and a new line followed by a blank line
becomes a paragraph break (</p>).
"""

Passing a None to linebreaksbr I was expecting to receive None. I read it
literally that it would add line breaks in plain text and if there was no plain
text then nothing would be done. Or at least if None is passed it would
return None.

For example:
{% with text=None %}
{% if text %}{{ text }}{% else %}-{% endif %}
{% endwith %}
<hr>
{% with text=None|linebreaksbr %}
{% if text %}{{ text }}{% else %}-{% endif %}
{% endwith %}

The second block prints "None" instead of "-".

The None gets converted to the string in the decorator 'stringfilter'.
The same happens in addslashes, capfirst, etc.

Should this be noted in the documentation? Or the behaviour changed? (hard
without backward compatibility issues). Or have I missed anything? Any
thoughts?

(I've checked the Django bug tracker and I haven't seen any bug describing
this)

Cheers,

--
Carles Pina i Estany
https://carles.pina.cat

Matthew Pava

unread,
Sep 14, 2020, 3:48:26 PM9/14/20
to django-d...@googlegroups.com
The filter states that it expects plain text. In Python, str(None) = "None". To get the behavior you are seeking use the default_if_none filter, and chain your other filters.
https://docs.djangoproject.com/en/dev/ref/templates/builtins/#default-if-none
{{ text|default_if_none:""|linebreaksbr }}

-----Original Message-----
From: django-d...@googlegroups.com <django-d...@googlegroups.com> On Behalf Of Carles Pina i Estany
Sent: Monday, September 14, 2020 2:40 PM
To: django-d...@googlegroups.com
Subject: Incomplete documentation or bug? (linebreaksbr and stringfilter decorated filters)


Hi,

Today I had a small surprise with templates and the linebreakbr filter.

The "problem" (or root of the confusion) is that if None (of NoneType) is given
to linebreakbr it returns a SafeString 'None' instead of None (type NoneType).

The documentation says (https://us-east-2.protection.sophos.com?d=djangoproject.com&u=aHR0cHM6Ly9kb2NzLmRqYW5nb3Byb2plY3QuY29tL2VuLzMuMS9yZWYvdGVtcGxhdGVzL2J1aWx0aW5zLyNzdGQ6dGVtcGxhdGVmaWx0ZXItbGluZWJyZWFrcw==&e=bWF0dGhldy5wYXZhQGlzcy5jb20=&t=eTYyb1Foa2VZVFJhdVU5dVBQazhvVFMycUpCMnZHSTFkZW9IOThqYVowWT0=&h=805589589ba040129c546a8f291d13ac):
https://us-east-2.protection.sophos.com?d=pina.cat&u=aHR0cHM6Ly9jYXJsZXMucGluYS5jYXQ=&e=bWF0dGhldy5wYXZhQGlzcy5jb20=&t=QUNiOTdmclhvZW9UZm4reEtEaExwOE9hM05Vc1dQN3VIc1ZISUtjWm1UQT0=&h=805589589ba040129c546a8f291d13ac

--
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 view this discussion on the web visit https://us-east-2.protection.sophos.com?d=google.com&u=aHR0cHM6Ly9ncm91cHMuZ29vZ2xlLmNvbS9kL21zZ2lkL2RqYW5nby1kZXZlbG9wZXJzLzIwMjAwOTE0MTk0MDE0LkdBMTkwMDclNDBwaW5hLmNhdA==&e=bWF0dGhldy5wYXZhQGlzcy5jb20=&t=MkRHTjRCUitxY0NPeUVSbmlRQi9ZMHRwclIrVFNhQ1lNTXlrQ1FSeGswRT0=&h=805589589ba040129c546a8f291d13ac.

Adam Johnson

unread,
Sep 14, 2020, 4:58:11 PM9/14/20
to django-d...@googlegroups.com
Yeah imo most filters should be worried about one type. We shouldn’t consider very variable “None-able” because it’s so pervasive. 

This is somewhere that type hints will eventually help clarify.

--
Adam

Carles Pina i Estany

unread,
Sep 15, 2020, 3:39:12 AM9/15/20
to django-d...@googlegroups.com

Hi,

Thanks Adam - type hints will help (specially if they can be represented
in the documentation), yes!

Matthew: thanks for the default_if_none tip!

For now I'll read the documentation in a different way regarding the
types :-) (well, it's easy now that I've seen the decorator
stringfilter!)

Thanks for the feedback,

Carles
> > https://groups.google.com/d/msgid/django-developers/b888ccf136b7465b9bf7863e7a423945%40iss2.ISS.LOCAL
> > .
> >
> > --
> Adam
>
> --
> 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 view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/CAMyDDM3Hzg%3DmUNzmsOPjLkO-%2BiNfUAeToY%3D7Kk3aUUAE5QobrA%40mail.gmail.com.
--
Carles Pina i Estany
https://carles.pina.cat
Reply all
Reply to author
Forward
0 new messages