I have never understood how the idea of extending classical particle Lagrangian mechanics to fields originated. The way I was first introduced to Lagrangian mechanics was by showing that the Lagrangian function shows up naturally by making use of the d'Alembert principle and Newtonian mechanics. Then, if we want to extend that to a continuous system (a string made up of infinitesimally close strings, for example), the Lagrangian density shows up naturally. However, this string, in the context of classical mechanics, still follows Newtonian mechanics.
Then how did we make this "philosophical leap" and think: "Well, fields must also follow this principle, as if they were a continuous set of... something?" Was it just by chance, or was someone simply trying to find a "master function" that, after minimising it, allowed us to find the differential equations governing a certain field? I'm just thinking right now about the classical electromagnetic Tensor, which seems to just be a "happy finding" and not something you derive. Maybe I'm just wrong.