Skip to content

Instantly share code, notes, and snippets.

@pharzan
Created September 28, 2017 10:17
Show Gist options
  • Save pharzan/e7df96897623bad062e7bcf54f6887cf to your computer and use it in GitHub Desktop.
Save pharzan/e7df96897623bad062e7bcf54f6887cf to your computer and use it in GitHub Desktop.
Django snippets
<!-- check to see if atheletes is a list in a template -->
{% if athlete_list %}
Number of athletes: {{ athlete_list|length }}
{% else %}
No athletes.
{% endif %}
So how can I question a variable that is a NoneType?
Use is operator, like this
''
if variable is None:
''
Why this works?
Since None is the sole singleton object of NoneType in Python, we can use is operator to check if a variable has None in it or not.
Quoting from is docs,
The operators is and is not test for object identity: x is y is true if and only if x and y are the same object. x is not y yields the inverse truth value.
Since there can be only one instance of None, is would be the preferred way to check None.
Comparisons to singletons like None should always be done with is or is not, never the equality operators.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment