python request properties and methods described

if  request.REQUEST.has_key('键值'):

HttpRequest object attributes

reference:

 

Table H-1. HttpRequest object attribute

Attributes

description

path

It represents the complete submission of the requested page address string, not including the domain name, such as "/ music / bands / the_beatles /".

method

Represents submit a request using the HTTP method. It is always capitalized. E.g:

if request.method == 'GET':

    do_something()

elif request.method == 'POST':

    do_something_else()

GET

A dictionary-like object containing all HTTP GET parameters of. See QueryDict document.

POST

A dictionary-like object containing all the HTTP POST parameters of. See QueryDict document.

By POST request submitted it may contain an empty POST dictionary that is, a form submitted by the POST method might not contain data. Thus, should not be used to determine ifrequest.POST using the POST method, but the use if request.method == "POST" (method entry in the table).

Note: POST does not include file upload information. See FILES.

REQUEST

For convenience, this is a dictionary-like object that searches POST, then search GET. Inspired by PHP's $ _REQEUST.

例如, 若 GET = {"name": "john"}, POST = {"age": '34'} ,REQUEST["name"] 会是 "john" , REQUEST["age"] 会是 "34" 。

It is strongly recommended to use GET and POST, rather than REQUEST. This is the former are more explicit.

COOKIES

A standard Python dictionary containing all cookie. Keys and values ​​are strings. For more information about cookie use, see Chapter 12.

FILES

A dictionary-like object containing all uploaded files. FILES key from <input type = "file" name = "" /> in the name. FILES value is a standard Python dictionary, the following three keys:

filename: string representing the file name of the uploaded file.

content-type: the content type of the uploaded file.

content: original content uploaded file.

Note that the only method FILES request is POST, <form> and submitted includes enctype = "multipart / form-data" if it contains data. Otherwise, FILES will be a blank dictionary-like object.

META

A standard Python dictionary containing all available HTTP headers. Valid header information associated with the client and the server. Here are a few examples:

CONTENT_LENGTH: Specifies contained in the request or response byte length of the data.

CONTENT_TYPE: indicated transmit or receive MIME type of entity.

QUERY_STRING: unparsed string of the original request.

REMOTE_ADDR: client IP address.

REMOTE_HOST: client host name.

SERVER_NAME: Server host name.

SERVER_PORT: server port number.

In any valid key META HTTP header information is a prefix HTTP_ with, for example:

HTTP_ACCEPT_ENCODING: encoding mechanism can understand the definition of the client.

HTTP_ACCEPT_LANGUAGE: custom client willing to accept natural language list.

HTTP_HOST: Host header sent by the client.

HTTP_REFERER: page pointed to, ifexisting.

HTTP_USER_AGENT: user-agent string client.

HTTP_X_BENDER : X-Bender 头信息的值,如果已设的话。

user

一个 django.contrib.auth.models.User 对象表示当前登录用户。 若当前用户尚未登录, user 会设为 django.contrib.auth.models.AnonymousUser 的一个实例。可以将它们与 is_authenticated() 区别开:

if request.user.is_authenticated():

    # Do something for logged-in users.

else:

    # Do something for anonymous users.

user 仅当Django激活 AuthenticationMiddleware时有效。

关于认证和用户的完整细节,见第12章。

session

一个可读写的类字典对象,表示当前session。仅当Django已激活session支持时有效。见第12章。

raw_post_data

POST的原始数据。 用于对数据的复杂处理。

Request对象同样包含了一些有用的方法,见表H-2。

表 H-2. HttpRequest 的方法

方法

描述

__getitem__(key)

请求所给键的GET/POST值,先查找POST,然后是GET。若键不存在,则引发异常 KeyError。

该方法使用户可以以访问字典的方式来访问一个 HttpRequest实例。

例如, request["foo"] 和先检查 request.POST["foo"] 再检查request.GET["foo"] 一样。

has_key()

返回 True 或 False,标识 request.GET 或 request.POST 是否包含所给的键。

get_full_path()

返回 path ,若请求字符串有效,则附加于其后。例如,"/music/bands/the_beatles/?print=true"。

is_secure()

如果请求是安全的,则返回 True 。也就是说,请求是以HTTPS的形式提交的。




QueryDict 对象

在一个 HttpRequest 对象中, GET和POST属性都是 django.http.QueryDict 的实例. QueryDict 是一个类似字典的类,被设计成可以处理同一个键有多个值的情况.这是很必要的,因为有些 HTML 表单元素,特别是``<select multiple="multiple">``,使用一个键来传递多个值

QueryDict 实例是不可变对象,除非你创建他们的一个拷贝.这意味着你不能直接改变 request.POST 和 request.GET 的值.

QueryDict 实现了所有的标准字典方法,因为它就是 dictionary 的一个子类.下文中对与标准字典不一致的地方做了标注:

        __getitem__(key) -- 返加给定键的值. 如果该键有多个值, __getitem__ 返回最后一个值.

        __setitem__(key, value) -- 将 key 的值设置为 [value] (一个Python 列表,只有一个元素 value).注意,这个方法象其它字典方法一个拥有副作用,只能被一个可变的 QueryDict 对象调用.(一个通过`` copy()``创建的副本).

        __contains__(key) -- 如果给定键存在,返回 True. 它允许你这么干: if "foo" in request.GET.

        get(key, default) --类似 __getitem__() ,如果该键不存在,返回一个默认值.

        has_key(key)

        setdefault(key, default) -- 类似标准字典的 setdefault(),不同之处在于它内部使用的是 __setitem__().

        update(other_dict) -- 类似标准字典的 update(), 唯一的不同是它将 other_dict 的元素追加到(而不是替换到)当前字典中. 示例:

        >>> q = QueryDict('a=1')
        >>> q = q.copy() # to make it mutable
        >>> q.update({'a': '2'})
        >>> q.getlist('a')
        ['1', '2']
        >>> q['a'] # returns the last
        ['2']

        items() -- 类似标准字典的 items() 方法, 类似 __getitem__() 的逻辑,它使用最后一个值. 示例:

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

        values() -- 类似标准字典的 values() 方法,类似 __getitem__() 的逻辑,它使用最后一个值.示例:

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

除了这些之外,``QueryDict`` 还拥有下列方法:

        copy() -- 返回当前对象的一个拷贝,它使用标准库中的 深拷贝 方法. 这个拷贝是可变的,也就是说你可以改变这个拷贝的值.

        getlist(key) -- 以一个Python列表的形式返回指定键的值.若该键不存在,返回一个空的列表.该列表是以某种方式排序的.

        setlist(key, list_) -- 不同于 __setitem__() ,将给定的键的值设置为一个列表.

        appendlist(key, item) -- 将给定键对应的值(别忘了,它是一个列表)追加一个 item.

        setlistdefault(key, default_list) -- 就象 setdefault ,不过它接受一个列表作为值而不是一个单一的值.

        lists() -- 就象 items(),不过它包含所有的值(以列表的方式):

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

        urlencode() -- 以一个查询字符串的形式返回一个字符串. Example: "a=2&b=3&b=5".

发布了106 篇原创文章 · 获赞 139 · 访问量 40万+

Guess you like

Origin blog.csdn.net/mingyuli/article/details/104087272