A specialized MultiValueDict which represents a query string. A QueryDict can be used to represent GET or POST data. It subclasses MultiValueDict since keys in such data can be repeated, for instance in the data from a form with a <select multiple> field. By default QueryDicts
| 571 | |
| 572 | |
| 573 | class QueryDict(MultiValueDict): |
| 574 | """ |
| 575 | A specialized MultiValueDict which represents a query string. |
| 576 | |
| 577 | A QueryDict can be used to represent GET or POST data. It subclasses |
| 578 | MultiValueDict since keys in such data can be repeated, for instance |
| 579 | in the data from a form with a <select multiple> field. |
| 580 | |
| 581 | By default QueryDicts are immutable, though the copy() method |
| 582 | will always return a mutable copy. |
| 583 | |
| 584 | Both keys and values set on this class are converted from the given |
| 585 | encoding (DEFAULT_CHARSET by default) to str. |
| 586 | """ |
| 587 | |
| 588 | # These are both reset in __init__, but is specified here at the class |
| 589 | # level so that unpickling will have valid values |
| 590 | _mutable = True |
| 591 | _encoding = None |
| 592 | |
| 593 | def __init__(self, query_string=None, mutable=False, encoding=None): |
| 594 | super().__init__() |
| 595 | self.encoding = encoding or settings.DEFAULT_CHARSET |
| 596 | query_string = query_string or "" |
| 597 | parse_qsl_kwargs = { |
| 598 | "keep_blank_values": True, |
| 599 | "encoding": self.encoding, |
| 600 | "max_num_fields": settings.DATA_UPLOAD_MAX_NUMBER_FIELDS, |
| 601 | } |
| 602 | if isinstance(query_string, bytes): |
| 603 | # query_string normally contains URL-encoded data, a subset of |
| 604 | # ASCII. |
| 605 | try: |
| 606 | query_string = query_string.decode(self.encoding) |
| 607 | except UnicodeDecodeError: |
| 608 | # ... but some user agents are misbehaving :-( |
| 609 | query_string = query_string.decode("iso-8859-1") |
| 610 | try: |
| 611 | for key, value in parse_qsl(query_string, **parse_qsl_kwargs): |
| 612 | self.appendlist(key, value) |
| 613 | except ValueError as e: |
| 614 | # ValueError can also be raised if the strict_parsing argument to |
| 615 | # parse_qsl() is True. As that is not used by Django, assume that |
| 616 | # the exception was raised by exceeding the value of max_num_fields |
| 617 | # instead of fragile checks of exception message strings. |
| 618 | raise TooManyFieldsSent( |
| 619 | "The number of GET/POST parameters exceeded " |
| 620 | "settings.DATA_UPLOAD_MAX_NUMBER_FIELDS." |
| 621 | ) from e |
| 622 | self._mutable = mutable |
| 623 | |
| 624 | @classmethod |
| 625 | def fromkeys(cls, iterable, value="", mutable=False, encoding=None): |
| 626 | """ |
| 627 | Return a new QueryDict with keys (may be repeated) from an iterable and |
| 628 | values from value. |
| 629 | """ |
| 630 | q = cls("", mutable=True, encoding=encoding) |
no outgoing calls