#31024 closed Cleanup/optimization (fixed)
firstof template tag documentation incorrectly specifies value testing.
Reported by: | Keryn Knight | Owned by: | Uttam Kini |
---|---|---|---|
Component: | Documentation | Version: | dev |
Severity: | Normal | Keywords: | |
Cc: | Triage Stage: | Ready for checkin | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | yes | UI/UX: | no |
Description
The documentation for {%firstof x y z %}
says:
Outputs the first argument variable that is not False. Outputs nothing if all the passed variables are False.
The emphasis not mine, and indicates it's specifically testing for the singleton False. But the example it gives as an equivalent is based on normal truth value testing:
{% if var1 %} {{ var1 }} {% elif var2 %} {{ var2 }} {% elif var3 %} {{ var3 }} {% endif %}
and the code is also based on truthiness
The documentation for how a simple {% if ... %}
behaves meanwhile more correctly prescribes it's function related to "true" and what that means.
evaluates a variable, and if that variable is “true” (i.e. exists, is not empty, and is not a false boolean value) the contents of the block are output
Were it actually based on False rather than Falsiness, the following would not output truthy.
>>> from django.template import Template, Context >>> context = {'a': None,'b': False,'c': 0,'d': '','e': (),'f': [],'g': {},'h': set(),'i': "truthy"} >>> t = Template("{% firstof a b c d e f g h i %}") >>> t.render(Context(context)) 'truthy'
Suggestion is to clarify that it's based on truthiness/falsiness, rather than imply it's directly based on encountering False.
Change History (7)
comment:1 by , 5 years ago
Easy pickings: | set |
---|---|
Summary: | firstof template tag documentation incorrectly specifies value testing → firstof template tag documentation incorrectly specifies value testing. |
Triage Stage: | Unreviewed → Accepted |
comment:2 by , 5 years ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
comment:3 by , 5 years ago
comment:4 by , 5 years ago
Has patch: | set |
---|---|
Patch needs improvement: | set |
comment:5 by , 5 years ago
Patch needs improvement: | unset |
---|---|
Triage Stage: | Accepted → Ready for checkin |
Pull request: https://github.com/django/django/pull/12162