Python ordered query parameters using urlencode

Order query parameters using urlencode.

from collections import OrderedDict
 
from django.utils.http import urlencode
 
params = {
    'a': 1,
    'b': 2,
    'c': 3,
}
print(urlencode(OrderedDict(params))) # Incorrect.

Unordered query parameters:

a=1&c=3&b=2

Ordered query parameters:

params = [
('a', 1),
('b', 2),
('c', 3),
]
print(urlencode(OrderedDict(params))) # Correct.
a=1&b=2&c=3
If you liked this posting, please feel free to leave me a comment below.
View this page on GitHub.
Posted .

Comments

Leave a Reply