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

梦想与她 提交于 2021-02-07 12:32:57

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!