During Tang dynsty and Song dynasty, Yunnan was not part of their territory. However, it was not a land without king. The birth of Nanzhao kingdom should to some extent owe the credit to Tang court. During Tang dynasty, From the Sui to the early Tang Dynasty, there were mang tribes living around the Erhai Lake region, which were existing in parallel. There were six most powerful ones. In the early Tang Dynasty, this region became a strategic area, because it was caught in the middle of Tang power and Tubo power, the relationship between them were highly strained. In order to pin dowm/contain the Tubo power, Tang court decided to foster a dominator over the chaotic area. The tribe iin the south county of Weishan called Mengshe, also called South tribe won the favor of Tang. With the help of Tang court as well as its facorable geological location, Nanzhao defeated the other five tribes and unifed the Lake Erhai region.
The establishment of Nanzhao regime was actually a strategy employed by the Tang Court to draw over Nanzhao kingdom to its side so as to contain Tupo regime. However, being only a tributary was never Nanzhao's intention, what they truly wanted was an independent and powerful kingdom. When Nanzhao kingdom was attempting to expand its territory by eradicating the powerful Cuan family and take over its land, Tang court smelt its ambition. As a result, the central court established a resident office in Yaoan county to control their military movement. Before, conflicts became too intense to be ignored and wars broke out between Tang and Nanzhao.
As a small kingdom, Nanzhao was to weak to counter Tang, so they allied with Tang's rival Tubo power from Tibetan Plateau. Tang court suffered a crushing defeat for twice. Under such circumstance, Tang court found it a wiser solution to draw Nanzhao over back to its side. The rebuilt alliance between Tang court and Nanzhao greatly weakened Tubo's power. After the Tang court and Nanzhao regime buried the hatchet, Tang court offered the chance for Nanzhao people to study for free in Chengdu, which greatly boosted the cultural development in Yunnan. The Nanzhao kingdom was perished due the usurpation by powerful officers.
The Nanzhao Kingdom lasted for 253 years, and was ruled by 13 kings, almost the similar life span to the Tang Dynasty in Chinese history. The capital of Nanzhao Kingdom was in the nowadays Dali Ancient Town.
After the perish of Nanzhao kingdom, Dali kingdom was not established immediately. Instead, three short-lived regimes appeared one after another. Their life were as short as 36 years. After 36 years of unrest and turmoil, Dali kingdom was eventually established by Duan Siping who ended the brief turmoil.
The Dali kingdom coexisted with Song dynasty and had pretty much similar life span. Unlike the harmonious relationship between Tang court and Nanzhao regime, the Song court was reluctant to accept Dali kingdom as its tributary. Partly because Song court was not as powerful as Tang court and they perceived Dali kingdom as potential threat. Emperor Huizong of Song Dynasty even drew a line over the Dadu 大渡River, saying that the area beyond the line did not belong to my territory. Thus, excluding Dali Kingdom. After years of request, Song dynasty finally accepted Dali kingdom as its vassal state. Even so, their vigilance towards Dali kingdom did not decrease. When Dali kingdom was paying tribute to Song court, they only accepted horses but turned down elephants. Because elephants stand for subordination to Song.
The Dali kingdom lasted for 315 years with 22 successive kings., 9 of them abdicated the throne and became monks in CHongsheng temple. Such phenomenon is absolutely one of its kind, from which we can discern the prevalence of Buddhism back then. It's capital was also in Dali.