Django-QueryDict object, django-querydict

Source: Internet
Author: User

Django-QueryDict object, django-querydict

InHttpRequestIn the object, the GET and POST attributes GET bothDjango. http. QueryDictThe created instance. This is a dictionary-like class customized by django. It is used to process multiple values with the same key.

In the original python dictionary, when a key has multiple values, a conflict occurs and only the last value is retained. In an HTTL form, a key usually has multiple values, for example<Select multiple>(Multiple selection box) is a common situation.

  Request. POSTAndRequest. GETOfQueryDictIt is immutable in a normal request/response loop. To obtain a variable version, use. Copy () method.

Next let's take a look at the methods in this class:

1.QueryDict._ Init __(Query_string = None, mutable = False, encoding = None)

This is a constructor, where query_string requires a string, for example:

>>> QueryDict('a=1&a=2&c=3')<QueryDict: {'a': ['1', '2'], 'c': ['3']}>

 

If query_string is not input, an empty object is obtained.

The QueryDict object you encounter, especially the one obtained by request. POST and request. GET. If you want to instantiate an object by yourself, you can pass mutable = True to make the object you instantiate changeable. Of course, request. POST and request. GET are created by django. That is to say, they are immutable unless the django source code is changed.

The keys and values are transcoded from encoding to Unicode. That is to say, if the input string query_string is GBK or UTF-8 encoded, it will be automatically transcoded to Unicode and then used as the dictionary key and value. If encoding = None, that is, it is not setDEFAULT_CHARSET value. Default Value: 'utf-8 '.

2.QueryDict.__getitem__(key)

Returns the value of the given key. If the key has multiple values,_ Getitem __()ReturnLast(Latest) value. If the key does not existDjango. utils. datastructures. MultiValueDictKeyError. (It is a Python StandardKeyErrorSo you can continue to captureKeyError.)

 

3.QueryDict._ Setitem __(Key, value)

Set the value of the given key[Value](A Python list with only one element value ). Note: Only objects that can be changed can be used, for example, objects created using the. copy () method.

 

4.QueryDict._ Contains __(Key)

If the given key has been set, returnTrue. It allows you to perform operations such as if "foo" in request. GET.

 

5.QueryDict.Get(Key, default)

  Usage_ Getitem __()The same logic, but a default value is returned if the key does not exist.

 

6.QueryDict.Setdefault(Key, default)

Similar to the Standard DictionarySetdefault ()Method, but it is used internally_ Setitem __(). That is to say, if the key already exists, its value is returned. If the key does not exist, default is returned, and both key and default are added to the object.

 

7.QueryDict.Update(Other_dict)

ReceiveQueryDictOr a standard dictionary. Similar to the Standard DictionaryUpdate ()Instead of replacing them.

For example:

>>> Q = QueryDict ('a = 1', mutable = True) # Of course, it must be variable before use >>> q. update ({'A': '2'}) >>> q. getlist ('A') ['1', '2'] >>> q ['a'] # returns the last ['2']

 

 

8.QueryDict.Items()

Similar to the Standard DictionaryItems ()Method, returns a list of the ancestor composed of key values. However, it uses_ Getitem __Returns the logic of the latest value.

For example:

>>> q = QueryDict('a=1&a=2&a=3')>>> q.items()[('a', '3')]

 

 

9.QueryDict.Iteritems()

Similar to the Standard DictionaryIteritems ()Method to return an iteration object. SimilarQueryDict. items (), It uses andQueryDict. _ getitem __()The same logic returns the latest value.

 

10.QueryDict.Iterlists()

SimilarQueryDict. iteritems ()Returns an iterator object that contains key-value pairs. value is a list of all key values.

 

11.QueryDict.Values()

Similar to the Standard DictionaryValues ()Method, but it uses_ Getitem __Returns the logic of the latest value. That is, a list of the latest values corresponding to all keys is returned.

For example:

>>> q = QueryDict('a=1&a=2&a=3')>>> q.values()['3']

 

 

12.QueryDict.Itervalues()

  SimilarQueryDict. values ()But it returns an iterator.

 

13.QueryDict.Copy()

Returns a copy of the object, usingCopy. deepcopy (). This copy is variable even if the original object is immutable.

 

14.QueryDict.Getlist(Key, default)

Return the data of the requested key in the form of a Python list. If the key does not exist and the default value is not provided, an empty list is returned. It ensures that a list of some types is returned, unless the default value is not a list.

 

15.QueryDict.Setlist(Key, list _)

Set for the given keyList _(_ Setitem __()), You can set a list of multiple elements.

 

16.QueryDict.Appendlist(Key, item)

Append an item to the list associated with the Internal Key.

 

17.QueryDict.Setlistdefault(Key, default_list)

SimilarSetdefault, Except that it accepts a list instead of a single value.

 

18.QueryDict.Lists()

SimilarItems, But it uses every Member in the dictionary as the list. That is to say, each element in the list is a binary ancestor composed of keys and corresponding value lists.

For example:

>>> q = QueryDict('a=1&a=2&a=3')>>> q.lists()[('a', ['1', '2', '3'])]

 

 

19.QueryDict.Pop(Key)

ReturnsListAnd remove them from the dictionary. If the key does not existKeyError.

For example ︰

>>> q = QueryDict('a=1&a=2&a=3', mutable=True)>>> q.pop('a')['1', '2', '3']

 

 

20.QueryDict.Popitem()

Delete dictionaryArbitraryA member (because there is no concept of order), and returns a list Of all values of the key and key, containing the binary tuples. When an empty dictionary is raisedKeyError.

For example ︰

>>> q = QueryDict('a=1&a=2&a=3', mutable=True)>>> q.popitem()('a', ['1', '2', '3'])

 

 

21.QueryDict.Dict()

ReturnQueryDictOfDictRepresentation. ForQueryDictEach (key, list) pair in,DictThere will be (key, item) pairs, where item is an element in the list, usingQueryDict. _ getitem __()The same logic is the latest:

>>> q = QueryDict('a=1&a=3&a=5')>>> q.dict(){'a': '5'}

 

 

22.QueryDict.Urlencode([Safe])

Returns the query string format from the data.

For example:

>>> q = QueryDict('a=2&b=3&b=5')>>> q.urlencode()'a=2&b=3&b=5'

 

(Optional) urlencode can pass characters that do not require encoding. (This means url encoding is required)

For example ︰

>>> q = QueryDict(mutable=True)>>> q['next'] = '/a&b/'>>> q.urlencode(safe='/')'next=/a%26b/'

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.