Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VariableDoesNotExist: Failed lookup for key [val2] in u'None'

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.

like image 693
Seán Hayes Avatar asked Mar 04 '16 03:03

Seán Hayes


1 Answers

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 %}
like image 139
Seán Hayes Avatar answered Oct 12 '22 14:10

Seán Hayes