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))) # a=1&c=3&b=2 (incorrect)
 
params = [
  ('a', 1),
  ('b', 2),
  ('c', 3),
]
print(urlencode(OrderedDict(params))) # a=1&b=2&c=3 (correct)
Did this help you? Please let me know with a comment. Thank you
Posted .

Comments (RSS)

Leave a comment