If I were going to go about nation building, I would “export democratic values” some 20 years in advance of any military action by way of film, literature, etc. Cynics can think of this as propaganda, but really it’s just giving Afghans or whomever a more accurate taste of western life. When enough people had a good idea about what democracy was concretely and were ready to support it, I would then provide for military support with the expectation that the democrats would need sustained assistance for many decades.
Of course, there might be lots of reasons why this still isn’t a good idea, especially that we might be able to do more good per dollar elsewhere. But I don’t think “we tried it (poorly) for 20 years and it didn’t work out” is a good criticism—we shouldn’t expect the kind of requisite culture change to happen over a single generation.
Germany and Japan were occupied (excluding the post-1955 nominal occupation of Germany because the Cold War prevented agreement on a formal end) for less time than Afghanistan when you add them together. To the extent something worked there but not in Afghanistan, it had nothing to do with it taking “generations”.
Germany and Japan were also quite a bit more successful at actually ending the organizations occupying troops found unacceptable.