In the HttpRequest object, both the property Get and POST get the instance created by django.http.QueryDict . This is a Django-custom dictionary-like class used to handle the same key with multiple values.
In the original Python dictionary, conflicts occur when a key has multiple values, leaving only the last value. In HTTL forms, it is common to have multiple values for a key, such as <select multiple> (multi box).
request. POST and request. the querydict of GET is immutable in a normal request/response loop. to get a mutable version, you need to use the . Copy () method.
Let's look at some of 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 the query_string is not passed in, an empty object is obtained.
The Querydict object you meet, especially the request. POST and request. Get the. If you want to instantiate an object yourself, you can pass mutable=true to make the object you are instantiating mutable. Of course request. POST and request. GET is created by Django, which means that they are immutable unless you change the Django source.
The keys and values that are set are transcoded from encoding to Unicode. That is, if the passed-in 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, which is not set, it will be usedDEFAULT_CHARSET 的值,默认为:‘utf-8‘。
2.QueryDict.__getitem__(key)
returns the value of the given key. If key has more than one value,__getitem__ () returns the last (most recent) value. If key does not exist, Django.utils.datastructures.MultiValueDictKeyErroris raised. (It is a subclass of the Python standard keyerror , so you can still persist in capturing keyerror.) )
3.querydict. __setitem__ (Key, value)
Set the value of the given key to [value](a python list with only one element value). Note: objects created by the. Copy () method can be used only when the object is available for change.
4.querydict. __contains__ (key)
Returns Trueif the given key is already set. It allows you to do if "foo" in Request. GET such an operation.
5.querydict. Get (Key, default)
use with the above __getitem__ () the same logic, but returns a default value when key does not exist.
6.querydict. SetDefault (Key, default)
A SetDefault () method that resembles a standard dictionary, except that it uses __setitem__ ()internally. That is, when the key already exists, return its value, key does not exist, return to default, and add key and default to the object.
7.querydict. Update (other_dict)
Receive a querydict or Standard dictionary. the update () method similar to the standard dictionary, but it is appended to the current dictionary item instead of replacing them.
For example:
>>> q = querydict ('a=1', mutable=true)#Of course, the variable can be used.>>> Q.update ({'a':'2'})>>> Q.getlist ('a')['1','2']>>> q['a']#returns the last['2']
8.querydict. Items ()
The items () method, similar to the standard dictionary, returns a list of Ganso that are composed of key values. But it uses the same logic as __getitem__ to return the most recent value.
For example:
>>> q = querydict ('a=1&a=2&a=3')>>> Q.items () [('a'3')]
9.querydict. Iteritems ()
A Iteritems () method similar to the standard dictionary that returns an iterative object. similar to querydict.items (), it uses the same logic as querydict.__getitem__ () to return the most recent value.
Ten.querydict. iterlists ()
Similar to querydict.iteritems (), returns a Ganso (key, value) iteration object that contains a key-value pair, and value is a list of values that include all keys.
One by one.querydict. Values ()
The values () method, similar to the standard dictionary, but it uses the same logic as __getitem__ to return the most recent value. That is, returns a list of the most recent values for all keys.
For example:
>>> q = querydict ('a=1&a=2&a=3')>>> Q.values () ['3']
querydict. itervalues ()
similar querydict.values () , except that it returns an iterator.
querydict . Copy ()
returns a copy of the object, using copy.deepcopy ()in the Python standard library. This copy is mutable even if the original object is immutable.
querydict . GetList (Key, default)
returns the data for the requested key as a Python list. If the key does not exist and no default value is provided, an empty list is returned. It guarantees that a list of some type is returned, unless the default value is not a list.
querydict. setlist (Key, List_)
Set list_(unlike __setitem__ () for a given key, you can set a multi-element list.
querydict. appendlist (Key, item)
Appends an item to the list that is internally associated with the key.
querydict . Setlistdefault (Key, Default_list)
Similar to SetDefault, except that it accepts a list instead of a single value.
querydict. lists ()
Similar to items, except that it takes each member of the dictionary as a list. That is, each element in the list is a two-dollar ancestor consisting of a key and a corresponding list of values.
For example:
>>> q = querydict ('a=1&a=2&a=3')>>> q.lists () [( 'a', ['1'2' '3'])
querydict. Pop (key)
Returns a list of values for the given key and removes them from the dictionary. If the key does not exist, Keyerroris raised.
For example
>>> q = querydict ('a=1&a=2&a=3', mutable=True)>>> Q.pop ('a') ['1'2' '3']
querydict. Popitem ()
Remove any member of the dictionary (because there is no sequential concept) and return a binary tuple that contains a list of all values for the key and key. Keyerroris thrown when called on an empty dictionary.
For example
>>> q = querydict ('a=1&a=2&a=3', mutable=True)>>> Q.popitem () ('a', ['1'2 ' ' 3 '])
querydict . Dict ()
return querydict dict representation. querydict each (key, list) pair, dict will have (key, item) pair, where item is an element in the list, using the TT class= "xref py py-meth docutils literal" >querydict.__getitem__ () the same logic, which is the latest:
>>> q = querydict ('a=1&a=3&a=5')>>> q.dict () { 'a'5'}
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'
Optionally, UrlEncode can pass characters that do not need to be encoded. (This means URL encoding)
For example
>>> q = querydict (mutable=True)>>> q['next'/a &b/'>>> q.urlencode (safe='/')' next=/a%26b/'
Django-querydict Object