The 20 year period between 1820 and 1840 was known in Spain as the 'Age of the Captive Kings'.
Ferdinand VII was restored to the Spanish throne by the Congress of Vienna, but by 1820 his erraticism, misrule, and complete failure to reform led to a revolt by liberal elements joined by the pragmatists of the center and even the right, many of them veterans of the
Peninsular War. Ferdinand's supporters on the far right and the royalist purists were overthrown and a reform minded Cortes formed. Ferdinand VII was made to accept a liberal constitution and was thereafter held a virtual (though comfortable) prisoner of his own government, as as his heir apparent, his brother
Carlos. King Ferdinand VII's appeals to the Great Powers fell upon deaf ears at the time, and Ferdinand turned inward, essentially becoming a dissipated hedonistic wretch, dying in 1829.
His successor, Carlos Vth, was if anything even more wed to the principals of the divine right of kings, but where Ferdinand was fickle and vindictive, Carlos was steadfast and pious. King Carlos V refused to acknowledge the constitutional limits of his kingship, but the Cortes feared to depose him and proclaim a republic, as this might be the one thing that would rouse the Great Powers from their non-interference. Over the course of 11 years, the King won the grudging respect of the Cortes, and the Cortes, which brought Spain back to a semblance of prosperity, won over Carlos V in the end. In 1840, a compromise was struck between the King and the Cortes, increasing the role of the King in government in return for his recognition of the Constitution de jure and not just de facto. In the 1840s, Spain drifted more to the right, and King Carlos V came to be seen as a strong ruler both in Spain and on the International stage, though he kept his word and never again threatened to overturn the Constitution of Spain.