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

On Sep/14/2020, Adam Johnson wrote:
> 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.
> 
> On Mon, 14 Sep 2020 at 20:47, Matthew Pava <[email protected]> wrote:
> 
> > 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: [email protected] <
> > [email protected]> On Behalf Of Carles Pina i Estany
> >
> > Sent: Monday, September 14, 2020 2:40 PM
> >
> > To: [email protected]
> >
> > 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
> > ):
> >
> > """
> >
> > 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://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 [email protected].
> >
> > 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
> > .
> >
> >
> >
> > --
> >
> > 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 [email protected].
> >
> > To view this discussion on the web visit
> > 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 [email protected].
> 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

-- 
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 [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-developers/20200915073833.GA17986%40pina.cat.

Reply via email to