Fixing KeyError exception when user is not logged in