问题
I was getting a VariableDoesNotExist error with the following snippet when obj1.page is None.
{{ obj1.val1|default:obj1.page.val2 }}
Normally Django templates don't care about attribute accesses on None values.
回答1:
Django only cares about attribute lookups on None values inside the default template filter. I got around it using:
{% with obj1.page.val2 as val2 %}
{{ obj1.val1|default:val2 }}
{% endwith %}
来源:https://stackoverflow.com/questions/35787497/variabledoesnotexist-failed-lookup-for-key-val2-in-unone