Template variable when None displays None

612 views
Skip to first unread message

Rand Bradley

unread,
Jul 11, 2007, 7:10:49 PM7/11/07
to django...@googlegroups.com
When a template variable value is None, is it expected to render the word 'None'? I would expect this to fail silently instead of displaying the word. Is there a configuration setting or some way to change the default rendering? Do I have to wrap variables with an {% if ... %} in order to not display None? I would appreciate any suggestions on how to handle the display of variables equal to None.

Thanks, Rand

Chris Brand

unread,
Jul 11, 2007, 7:15:30 PM7/11/07
to django...@googlegroups.com

When a template variable value is None, is it expected to render the word 'None'? I would expect this to fail silently instead of displaying the word. Is there a configuration setting or some way to change the default rendering? Do I have to wrap variables with an {% if ... %} in order to not display None? I would appreciate any suggestions on how to handle the display of variables equal to None.

Rand Bradley

unread,
Jul 11, 2007, 9:12:45 PM7/11/07
to Django users
The default_if_none filter was not working when I tried it the first
time, but I retried it after your post and it is working so this may
solve my issue.

Is that the designed behavior template variables? If the value is
None, display the word "None"? So someone has to use the
default_if_none filter or use a conditional expression in order to
render an empty string for all variables that have the potential value
of None? In my opinion, this seems counter-intuitive, and a better
approach might be to render an empty string by default, and use the
default_if_none filter to render the word 'None' if desired.

Nathan Ostgard

unread,
Jul 11, 2007, 11:38:27 PM7/11/07
to Django users
It's the default behavior of Python:

>>> str(None)
'None'

I don't agree that it should be the default. None and "" are two
distinct values -- especially with a database. In databases, None (or
NULL) normally represents a _missing_ value, whereas a "" is one that
was intentionally specified to be blank.

Reply all
Reply to author
Forward
0 new messages