QueryDict默认是不可变的,同过将QueryDict对象的_mutable 属性的值设置成True就可以为其赋值。QueryDict对象的urlencode()方法将QueryDict转换为字符串:

from django.http import QueryDict

q = QueryDict()
q._mutable = True
q['name'] = 'lcg'
print(q)  # <QueryDict: {'name': ['lcg']}>
print(q.urlencode())  # name=lcg

  

05-11 22:48