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

-- 
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-developers+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-developers/20200914194014.GA19007%40pina.cat.

Reply via email to