Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django setting for default template tag output when variable is None?

I am looking for a django setting or programmatic way to make all django template tags show the empty string when the value is None. For example, imagine that I have some django template:

{{cat}} chases {{mouse}} 

if both cat and mouse are None, it will render as:

None chases None 

I am aware that I can set each one using {{cat|default:""}} or {{mouse|default_if_none:""}}

However, I am looking for some sort of setting that would allow me to set the default for all tags, without explicitly adding |default:"" to every tag.

I am also aware of a setting called TEMPLATE_STRING_IF_INVALID. However, this setting applies only to invalid strings. None is considered valid.

like image 594
zzz Avatar asked Sep 28 '11 22:09

zzz


People also ask

What will insert template system in Django Temlates If you use a variable that doesn't exist?

If you use a variable that doesn't exist, the template system will insert the value of the string_if_invalid option, which is set to '' (the empty string) by default.

What does {{ name }} mean in Django templates?

What does {{ name }} this mean in Django Templates? {{ name }} will be the output. It will be displayed as name in HTML. The name will be replaced with values of Python variable.

What does {% %} mean in Django?

{% %} and {{ }} are part of Django templating language. They are used to pass the variables from views to template. {% %} is basically used when you have an expression and are called tags while {{ }} is used to simply access the variable.

How do you solve template does not exist in Django?

Django TemplateDoesNotExist error means simply that the framework can't find the template file. To use the template-loading API, you'll need to tell the framework where you store your templates. The place to do this is in your settings file ( settings.py ) by TEMPLATE_DIRS setting.

Does default_if_none have any use in Django templates?

Does default_if_none have any use in Django templates? Bookmark this question. Show activity on this post. Generally, if a variable doesn't exist, the template system inserts the value of the TEMPLATE_STRING_IF_INVALID setting, which is set to '' (the empty string) by default.

What to do if a variable doesn't exist in Django?

From the Django docs, Generally, if a variable doesn't exist, the template system inserts the value of the TEMPLATE_STRING_IF_INVALID setting, which is set to '' (the empty string) by default. Filters that are applied to an invalid variable will only be applied if TEMPLATE_STRING_IF_INVALID is set to '' (the empty string).

How do I define a custom template tag in Django?

To define a custom template tag, you specify how the compilation works and how the rendering works. When Django compiles a template, it splits the raw template text into ‘’nodes’’.

How do I find the parent template in Django?

If the variable evaluates to a string, Django will use that string as the name of the parent template. If the variable evaluates to a Template object, Django will use that object as the parent template. See Template inheritance for more information. Normally the template name is relative to the template loader’s root directory.


2 Answers

No such thing exists. That's why the default and default_if_none filters exist. This is a feature; it makes you think about what you're doing instead of relying on some behavior that would often times be misleading. If there's a potential for a variable to be None, then you should plan for that contingency. If the variable should always have some value, then the "None" indicates something is not right. If the default was to just render a blank string, then you would not know whether the value is not defined or is actually a blank string. Write coherent code and forget about shortcuts.

like image 71
Chris Pratt Avatar answered Oct 14 '22 04:10

Chris Pratt


"Explicit is better than implicit"

Think of how enraged you would be when things wouldn't render properly because you forgot that you had enabled the magic "render everything with a false value as a null string" setting.

If you find you're using the default_if_none filter a lot, you might want to consider changing casting None to '' BEFORE it's passed to the template.

Your template will be simpler, and you will have explicitly made this decision to stringify null values.

like image 39
jathanism Avatar answered Oct 14 '22 05:10

jathanism