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
This post is available on GitHub.
Posted .

Comments

Leave a Reply