In the context of template variable interpolation i.e. `{{ }}`
    syntax, it is defined that `x` in this case will get converted to
    the empty string (or your 'string_if_invalid' setting) -
    
https://docs.djangoproject.com/en/dev/ref/templates/api/#how-invalid-variables-are-handled
    
<https://docs.djangoproject.com/en/dev/ref/templates/api/#how-invalid-variables-are-handled>
    - unlike the case for `if` tags.

    When this value (the empty string) is passed to `default_if_none`,
    the value is not changed, since it is not None. See
    
https://docs.djangoproject.com/en/1.10/ref/templates/builtins/#default-if-none
    
<https://docs.djangoproject.com/en/1.10/ref/templates/builtins/#default-if-none>


The missing piece of information for me (and even reading the docs it's far from obvious) is that undefined variables mean two different things in the different contexts.

The value *is* changed in the second case, because it *is* None. Undefined values are '' in {{ }} context but are None in {% %} context. Maybe that's obvious to you, but it's not to me. In fact, after a few minutes searching I still can't find where this is mentioned in the documentation. Only the vague statement that invalid variables are "generally" replaced with string_if_invalid.

I meant above that in the {{ }} context 'default_if_none' doesn't return the default value, but just returns the empty string input value.

I also had to read the docs to understand the behaviour - it is mentioned in the 3rd paragraph down in the docs linked above, "How invalid variables are handled" - the `if`, `for` and `regroup` tags are specifically mentioned. I think the docs could be clearly about how things work with variables passed to template tags etc. "In general" as you say is rather vague. Definitely room for improvement here!


Luke

--
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 post to this group, send email to django-developers@googlegroups.com.
Visit this group at https://groups.google.com/group/django-developers.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-developers/49c674a7-ab69-cfd6-0fdc-9ba444fd1ffa%40cantab.net.
For more options, visit https://groups.google.com/d/optout.

Reply via email to