There are numerous ways to achieve contentment with the decree of Allah:

The servant having certainty in Allah and a firm trust that whatever He decrees for a believer will be good for him. As such he will be like a patient who has submitted to the ministrations of a skilled doctor: such a patient will be content with his ministrations be they painful or not because he has complete trust that the doctor is doing only that which will be of benefit to him.
Looking to the reward that Allah has promised for contentment. The servant could well be so engrossed in powering this that he forgets all about the pain he is facing. It is reported that a righteous woman from the Salaf tripped and broke a nail whereupon she laughed saying, ‘The delight of His reward has made me forget the bitterness of His pain.’
Immersing oneself in love of the One who sends tribulation, constantly being aware of His magnificence, beauty, greatness and perfection which is without limit. The potency of such awareness will cause the servant to drown in it such that he no longer senses pain much in the same way that the women who saw Yusuf forgot about the pain of cutting their hands. This is a higher station than those previously mentioned.
The point here is that the Prophet, may Allah’s peace and blessing be upon him, enjoined ibn Abbas to work deeds while in a state of contentment if he was able to do so. If not, he said,

“If you are unable, know that great good lies in bearing with patience what you dislike,”

this then proves that being content with decrees that are hard to bear is not an obligation but rather a recommendation, a state of excellence. Whoever is unable to be content must instead be patient. Patience is obligatory, it must be present, and it contains great good. Allah, Most High, has commanded patience and promised great reward for it:

“The patient will be paid their wages in full without any reckoning.” (al-Zumar 39:10)

“Give good news to the patient: those who, when disaster strikes them, say, ‘We belong to Allah and to Him we will return.’ Those are the people who will have blessings and mercy from their Lord; they are the ones who are guided.” (al-Baqarah 2:155-157)

“Give good news to the humble hearted, whose hearts quake at the mention of Allah, and who are patient in the face of all that happens to them.” (al-Hajj 22:34-35)

Al-Hasan said, ‘The state of contentment is rare, but patience is the recourse of the believer.’ (Abu Nu’aym) Sulayman al-Khawas said, ‘The station of patience is below that of contentment. Contentment is that a person, before the onset of tribulation, is content whether it is present or not. Patience is that a person, after the onset of tribulation, bears it steadfastly.’

The difference between patience and contentment is that patience is to restrain the soul and to prevent it from displeasure while sensing discomfort or pain. Contentment necessitates that the heart readily accept what it is facing and, even if it was to feel some pain at what it is facing, the sense of contentment will lessen it, perhaps even remove it altogether. This is because the heart has felt the soothing breath of certainty and cognisance.

This is why a large group of the Salaf such as Umar ibn Abdul Aziz, Fudayl, Abu Sulayman and ibn Mubarak would say, ‘The person who is content does not desire a state other than the one he is in whereas the patient does.’ This state of being is reported from a group of Companions, amongst whom were Umar and ibn Masud.

Abdul Aziz ibn Abu Ruwwad said, ‘Amongst the Children of Israel there was a devout worshipper who saw a dream in which he was told that so-and-so would be his wife in Paradise. So he went to her as a guest for three nights to see what she did. She would sleep while he prayed by night and she would eat while he fasted. When he left her, he asked her about the greatest deed she felt she did. She replied, ‘I do no more than what you have seen except that I have one quality: If I am in trying times, I do not want to be in times of ease. If I am ill, I do not wish to be healthy. If I am hungry, I do not wish to be full. And if I am in the sun, I do not wish to be in the shade.’ He said, ‘By Allah, this is equality that is beyond the reach of the servants!’

Patience is to be shown at the onset of calamity as is authentically reported from the Prophet, may Allah’s peace and blessing be upon him. (Bukhari) Contentment is shown after the onset of calamity as the Prophet, may Allah’s peace and blessing be upon him, said in his supplication,

“I ask You for contentment after the decree.” (Ahmad)

This is because a servant could well resolve to be content at the decree before it occurs, but the resolve dissipate when he actually faces it. Whoever is content after the decree has befallen is one who is truly content. (Khattabi Sha’n al-Du’a)