Querydict objects in Django.http.request

Source: Internet
Author: User

In a HttpRequest object, both the GET and POST properties are instances of django.http.QueryDict. Querydict is a dictionary-like class that is designed to handle multiple values with a single key. This class is needed when working with elements in HTML forms, especially <select multiple= "multiple" >, which pass multiple-valued elements of the same key.

The Querydict instance is immutable unless a copy () replica is created. This means that the request cannot be changed directly. POST and request. The properties of the GET.

Querydict implements the method of all standard dictionaries because it is a subclass of the dictionary. Something different is listed in the following table.

The difference between querydicts and standard dictionaries

Method

differs from the standard dictionary implementation

__getitem__

The same as a dictionary. However, when a key has more than one value, __getitem__ () returns the last value.

__setitem__

Set the value of the given key to [value] (a python list with only one value element). Note that because of the side effects on other dictionary functions, it can only be called a mutable querydict (created by copy ()).

Get ()

If a key is multiple values, like __getitem__, get () returns the last value.

update ()

update  of the Standard dictionary, this method * adds * instead of replacing one item:

>>> q = querydict (' a=1 ')

>>> Q = Q.copy () #  makes it variable

>>> q.update ({' A ': ' 2 '})

Span style= "font-size:12px" >>>> q.getlist (' a ')

[' 1 ', ' 2 ']

>>> q[' a '] #  returns the last value

[' 2 ']

Items ()

As with the standard dictionary's items () method, the difference is that it returns the last value, like __getitem () __:

>>> q = querydict (' a=1&a=2&a=3 ')

>>> Q.items ()

[(' A ', ' 3 ')]

VALUES ()

As with the values () method of the standard dictionary, the difference is that it returns the last value as __getitem () __.

Additional (non-dictionary) Querydict methods

Method

Describe

Copy ()

Returns a copy of an object using the Copy.deepcopy () in the Python standard library. The copy is mutable, meaning that you can change its value.

GetList (Key)

Returns the data for the requested key in the form of a Python list. Returns an empty list if the key does not exist. It guarantees that a certain form of list will be returned.

Setlist (key, List_)

Sets the key value of the given key to List_ (different from __setitem__ ()).

Appendlist (key, item)

Add item on key-related list.

Setlistdefault (Key, L)

As with SetDefault, the difference is that its second argument is a list, not a value.

Lists ()

As with items (), the difference is that it returns all the values of each member of the dictionary as a list. For example:

>>> q = querydict (' a=1&a=2&a=3 ')

>>> q.lists ()

[(' A ', [' 1 ', ' 2 ', ' 3 '])]

UrlEncode ()

Returns a string of data in the format of a request string (for example, "a=2&b=3&b=5").

Querydict objects in Django.http.request

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.