Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does default_if_none have any use in Django templates?

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). If TEMPLATE_STRING_IF_INVALID is set to any other value, variable filters will be ignored.

This behavior is slightly different for the if, for and regroup template tags. If an invalid variable is provided to one of these template tags, the variable will be interpreted as None. Filters are always applied to invalid variables within these template tags.

If an invalid variable always gets translated to '', for template tags and filters other than if, for and regroup, then what good does the template filter default_if_none do? Obsolete?

like image 642
tamakisquare Avatar asked May 12 '11 00:05

tamakisquare


People also ask

What does Django template include?

A Django template is a text document or a Python string marked-up using the Django template language. Some constructs are recognized and interpreted by the template engine. The main ones are variables and tags. A template is rendered with a context.

Which characters are illegal in template variable names in Django?

Variable names consist of any combination of alphanumeric characters and the underscore ( "_" ) but may not start with an underscore, and may not be a number.

Can we write Python code in Django template?

You cannot use python code in django template. This is by design, Django's idea of template is to isolate the presentation logic from the programming code.

Can you use Django without templates?

Yes, it is possible to do this. In order to render HTML without a template in Django, we'll need to manually build up a string of HTML and send that to the browser as an HttpResponse.


2 Answers

There is a difference between an invalid variable and one that exists but has a value of None.

Consider the following context:

{'apple':'green','banana':None}`

In your template {{ apple }} resolves to green, while {{ banana }} resolves to None, and {{ orange }} resolves to TEMPLATE_STRING_IF_INVALID.

Now consider {{ banana|default_if_none:'yellow' }} and you should see the use of the default_if_none tag.

like image 88
Wogan Avatar answered Oct 02 '22 08:10

Wogan


Here's a case where I have used default_if_none a few times. I'm querying a secondary database in which I have no control and I'm displaying the data in the template. Most of the times the data looks fine but sometimes, the data value will show None. In that case, I will use the filter as:

{{ data_value|default_if_none:"N/A" }}

The general public and users of site doesn't usually understand what the None value means, by replacing it with a more friendly word, the filter default_if_none comes in handy.

like image 34
Thierry Lam Avatar answered Oct 02 '22 07:10

Thierry Lam