• Aucun résultat trouvé

Accessing Submitted Data

Dans le document Pro Django (Page 180-183)

Any time a request comes in, it can potentially be accompanied by a variety of data provided by the Web browser. Processing this information is key to making a Web site dynamic and interactive, so Django makes it easy and flexible. Just as there are many ways to submit data to a Web server, there are as many ways to access that data once it arrives.

Data that comes in using the standard query- string format3 sent by most browsers is auto-matically parsed into a special type of dictionary class called Mqanu@e_p. This is an immutable subclass of IqhpeR]hqa@e_p, which means that it functions mostly like a dictionary, but with a few added options for handling multiple values for each key in the dictionary.

3. dppl6++lnk`f]jck*_ki+mqanu)opnejc+

The most significant detail of Mqanu@e_p is that it’s instantiated with a query- string from an incoming request. For more information on the details of how to access values in a Mqanu@e_p, see the details for IqhpeR]hqa@e_p in Chapter 9.

HttpRequest.GET

If the request came in with the GET method, its CAP attribute will be a Mqanu@e_p containing all the values that were included in the query- string portion of the URL. Of course, while there’s no technical restriction on when CAP can be used to get parameters out of a URL, the goal of clean URLs limits the situations where it’s most advantageous.

In particular, it’s important to separate parameters that identify a resource from those that customize how the resource is retrieved. This is a subtle, but important, distinction. Con-sider the following examples:

฀ ฀+^kkg+lnk)`f]jck+_d]lpan,3+

฀ ฀+jaso+.,,4+fqj+-1+sa^oepa)h]qj_da`+

฀ ฀+nalknp+.,,4+atlajoao+;kn`anejc9_]packnu

As you can see, most of the data sent to the view for GET requests should be placed in the URL itself, rather than the query- string. This will help search engines index them more effi-ciently, while also making it easier for users to remember them and communicate them with others. As with many other principles, this isn’t an absolute rule, so keep query- strings and the CAP attribute in your toolbox, but use them with care.

HttpRequest.POST

If the request comes in with a PUT or POST method using a standard HTML form, this will be a Mqanu@e_p containing all the values submitted with the form. The LKOP attribute will be popu-lated for all standard forms, regardless of the encoding type, with or without files.

However, the HTTP specification allows these requests to supply data in any format, so if the incoming data doesn’t fit the format of a query- string, DpplNamqaop*LKOP will be empty, and the data will have to be read in directly through DpplNamqaop*n]s[lkop[`]p].

HttpRequest.FILES

If an incoming PUT or POST request includes any uploaded files, those files will be stored away in the BEHAO attribute, which is also a Mqanu@e_p, with each value being an Qlhk]`a`Beha object.

Living at `f]jck*_kna*behao*qlhk]`a`beha, this is a subclass of the Beha object described in Chapter 9, providing a few extra attributes specific to uploaded files.

฀ ฀_kjpajp[pula—The ?kjpajp)Pula associated with the file, if any was provided. Web browsers typically assign this based on the last part of the filename, though a Web service call could specify this more accurately based on the actual type of content.

฀ ฀_d]noap—The character set that was specified for the uploaded file’s content.

HttpRequest.raw_post_data

Any time a request comes in with data in the body of the request, as is done for PUT and POST, the n]s[lkop[`]p] attribute provides access to this content, without any parsing. This isn’t

typically necessary for most Web sites, as the CAP and LKOP attributes are more appropriate for the most common types of requests. Web services may accept data in any format, and many use XML as a primary means of data transfer.

Dictionary Access

In addition to accessing CAP and LKOP separately, Django provides a shortcut for accessing val-ues provided with the reqval-uest, regardless of which method is used. To use this shortcut, simply access the request as a dictionary. This will check values in LKOP before CAP. Like any diction-ary, if a value can’t be found in either location, a GauAnnkn is raised.

It’s important to note that accessing values from the request as a dictionary doesn’t pro-vide the full functionality of IqhpeR]hqa@e_p. Instead, accessing a value by key will behave just like doing the same with a IqhpeR]hqa@e_p, returning the last instance of that value that was specified in the request.

:::bnki`f]jck*dppleilknpDpplNamqaop(Mqanu@e_p :::namqaop9DpplNamqaop$%

:::namqaop*CAP9Mqanu@e_p$#]9-"^9."^9/"_90#%

:::namqaop*LKOP9Mqanu@e_p$#^91"_92"`93"`94#%

:::namqaop*CAPW#]#Y(namqaop*CAPW#^#Y(namqaop*CAPW#_#Y

$q#-#(q#/#(q#0#%

:::namqaop*LKOPW#^#Y(namqaop*LKOPW#_#Y(namqaop*LKOPW#`#Y

$q#1#(q#2#(q#4#%

:::namqaop*CAP*capheop$#]#%(namqaop*CAP*capheop$#^#%

$Wq#-#Y(Wq#.#(q#/#Y%

:::namqaopW#]#Y(namqaopW#^#Y(namqaopW#_#Y(namqaopW#`#Y

$q#-#(q#1#(q#2#(q#4#%

HttpRequest.META

When a request comes in, there is a significant amount of information related to the request that doesn’t come through in a query- string and isn’t available in the CAP or LKOP attributes on the request. Instead, data regarding where the request came from and how it got to the server is stored in the request’s IAP= attribute. Details of which values are available in IAP= can be found in PEP- 333.

In addition, each request is accompanied by a number of headers, which describe various options the client would like to make known. Exactly what these types of headers can contain is specified in the HTTP specification,4 but they typically control things like a preferred lan-guage, allowable content- types and information about the Web browser.

These headers are also stored in IAP=, but in a form slightly altered from how they came in originally. All HTTP header names become uppercase, are prefixed with DPPL[ and have all of their dashes replaced with underscores.

฀ ฀Dkop becomes DPPL[DKOP.

฀ ฀Nabanan becomes DPPL[NABANAN.

฀ ฀T)Bkns]n`a`)Bkn becomes DPPL[T[BKNS=N@A@[BKN.

4. dppl6++lnk`f]jck*_ki+dppl)da]`ano+

HttpRequest.COOKIES

Since each HTTP request is a fresh connection between the client and the server, cookies are used as a way to identify clients that make multiple requests. In a nutshell, cookies are little more than a way to send a name and associated value to a Web browser, which that browser will then send back each time it makes a new request to the Web site.

While cookies are set during the response phase of the process, as documented under DpplNaolkjoa, the task of reading cookies from an incoming request is quite simple. The

?KKGEAO attribute of the request is a standard Python dictionary mapping names of cookies to the values that were previously sent.

Keep in mind that this dictionary will contain entries for all cookies sent by the browser, even if they were set by another application on the same server. The DpplNaolkjoa section later in this chapter covers the specific rules of how a browser decides which cookies to send with a particular request and how to control that behavior.

Dans le document Pro Django (Page 180-183)