Django Book 翻译
菜单>:
TOC
返回
原文:
.. table:: Table H-1. Attributes of HttpRequest Objects +-----------------+-----------------------------------------------------------+ |Attribute |Description | +=================+===========================================================+ |``path`` |A string representing the full path to the requested page, | | |not including the domain for example, | | |``"/music/bands/the_beatles/"`` . | +-----------------+-----------------------------------------------------------+ |``method`` |A string representing the HTTP method used in the request. | | |This is guaranteed to be uppercase. For example: | | | | | |:: | | | | | | | | | if request.method == 'GET': | | | do_something() | | | elif request.method == 'POST': | | | do_something_else() | | | | | |.. | | | | | | | +-----------------+-----------------------------------------------------------+ |``GET`` |A dictionary-like object containing all given HTTP GET | | |parameters. See the upcoming ``QueryDict`` documentation. | +-----------------+-----------------------------------------------------------+ |``POST`` |A dictionary-like object containing all given HTTP POST | | |parameters. See the upcoming ``QueryDict`` documentation. | | | | | |Its possible that a request can come in via POST with an | | |empty ``POST`` dictionary if, say, a form is requested via | | |the POST HTTP method but does not include form data. | | |Therefore, you shouldnt use ``if request.POST`` to check | | |for use of the POST method; instead, use ``if | | |request.method == "POST"`` (see the ``method`` entry in | | |this table). | | | | | |Note: ``POST`` does *not* include file-upload information. | | |See ``FILES`` . | +-----------------+-----------------------------------------------------------+ |``REQUEST`` |For convenience, a dictionary-like object that searches | | |``POST`` first, and then ``GET`` . Inspired by PHPs | | |``$_REQUEST`` . | | | | | |For example, if ``GET = {"name": "john"}`` and ``POST = | | |{"age": '34'}`` , ``REQUEST["name"]`` would be ``"john"`` ,| | |and ``REQUEST["age"]`` would be ``"34"`` . | | | | | |Its strongly suggested that you use ``GET`` and ``POST`` | | |instead of ``REQUEST`` , because the former are more | | |explicit. | +-----------------+-----------------------------------------------------------+ |``COOKIES`` |A standard Python dictionary containing all cookies. Keys | | |and values are strings. See Chapter 12 for more on using | | |cookies. | +-----------------+-----------------------------------------------------------+ |``FILES`` |A dictionary-like object containing all uploaded files. | | |Each key in ``FILES`` is the ``name`` from the ``
`` . Each value in ``FILES`` is a | | |standard Python dictionary with the following three keys: | | | | | |* ``filename`` : The name of the uploaded file, as a | | | Python string | | | | | |* ``content-type`` : The content type of the uploaded | | | file. | | | | | |* ``content`` : The raw content of the uploaded file. | | | | | |Note that ``FILES`` will contain data only if the request | | |method was ``POST`` and the ``
`` that posted to the | | |request had ``enctype="multipart/form-data"`` . Otherwise, | | |``FILES`` will be a blank dictionary-like object. | +-----------------+-----------------------------------------------------------+ |``META`` |A standard Python dictionary containing all available HTTP | | |headers. Available headers depend on the client and server,| | |but here are some examples: | | | | | |* ``CONTENT_LENGTH`` | | | | | |* ``CONTENT_TYPE`` | | | | | |* ``QUERY_STRING`` : The raw unparsed query string | | | | | |* ``REMOTE_ADDR`` : The IP address of the client | | | | | |* ``REMOTE_HOST`` : The hostname of the client | | | | | |* ``SERVER_NAME`` : The hostname of the server. | | | | | |* ``SERVER_PORT`` : The port of the server | | | | | |Any HTTP headers are available in ``META`` as keys prefixed| | |with ``HTTP_`` , for example: | | | | | |* ``HTTP_ACCEPT_ENCODING`` | | | | | |* ``HTTP_ACCEPT_LANGUAGE`` | | | | | |* ``HTTP_HOST`` : The HTTP ``Host`` header sent by the | | | client | | | | | |* ``HTTP_REFERER`` : The referring page, if any | | | | | |* ``HTTP_USER_AGENT`` : The clients user-agent string | | | | | |* ``HTTP_X_BENDER`` : The value of the ``X-Bender`` | | | header, if set | +-----------------+-----------------------------------------------------------+ |``user`` |A ``django.contrib.auth.models.User`` object representing | | |the currently logged-in user. If the user isnt currently | | |logged in, ``user`` will be set to an instance of | | |``django.contrib.auth.models.AnonymousUser`` . You can tell| | |them apart with ``is_authenticated()`` , like so: | | | | | |:: | | | | | | | | | if request.user.is_authenticated(): | | | # Do something for logged-in users. | | | else: | | | # Do something for anonymous users. | | | | | |.. | | | | | | | | | | | |``user`` is available only if your Django installation has | | |the ``AuthenticationMiddleware`` activated. | | | | | |For the complete details of authentication and users, see | | |Chapter 12. | +-----------------+-----------------------------------------------------------+ |``session`` |A readable and writable, dictionary-like object that | | |represents the current session. This is available only if | | |your Django installation has session support activated. See| | |Chapter 12. | +-----------------+-----------------------------------------------------------+ |``raw_post_data``|The raw HTTP POST data. This is useful for advanced | | |processing. | +-----------------+-----------------------------------------------------------+
翻译:
.. table:: 表 H-1. HttpRequest对象的属性 +-----------------+-----------------------------------------------------------+ |属性 |描述 | +=================+===========================================================+ | ``path`` |表示提交请求页面完整地址的字符串, | | |不包括域名,如 ``"/music/bands/the_beatles/"`` 。 | +-----------------+-----------------------------------------------------------+ |``method`` |表示提交请求使用的HTTP方法。 | | |它总是大写的。例如: | | | | | |:: | | | | | | | | | if request.method == 'GET': | | | do_something() | | | elif request.method == 'POST': | | | do_something_else() | | | | | |.. | | | | | | | +-----------------+-----------------------------------------------------------+ |``GET`` |一个类字典对象,包含所有的HTTP的GET参数的信息。 | | |见 ``QueryDict`` 文档。 | +-----------------+-----------------------------------------------------------+ |``POST`` |一个类字典对象,包含所有的HTTP的POST参数的信息。 | | |见 ``QueryDict`` 文档。 | | | | | |通过POST提交的请求有可能包含一个空的 ``POST`` 字典, | | |也就是说, 一个通过POST方法提交的表单可能不包含数据。 | | |因此,不应该使用 ``if request.POST`` 来判断POST方法的使用,| | |而是使用 ``if request.method == "POST"`` | | |(见表中的 ``method`` 条目)。 | | | | | |注意: ``POST`` 并 *不* 包含文件上传信息。 | | |见 ``FILES`` 。 | +-----------------+-----------------------------------------------------------+ |``REQUEST`` |为了方便而创建,这是一个类字典对象,先搜索 ``POST`` , | | |再搜索 ``GET`` 。 灵感来自于PHP的 ``$_REQEUST`` 。 | | | | | |例如, 若 ``GET = {"name": "john"}`` , ``POST = | | |{"age": '34'}`` , ``REQUEST["name"]`` 会是 ``"john"`` , | | |``REQUEST["age"]`` 会是 ``"34"`` 。 | | | | | |强烈建议使用 ``GET`` 和 ``POST`` ,而不是 ``REQUEST`` 。 | | |这是为了向前兼容和更清楚的表示。 | +-----------------+-----------------------------------------------------------+ |``COOKIES`` |一个标准的Python字典,包含所有cookie。 | | |键和值都是字符串。cookie使用的更多信息见第12章。 | +-----------------+-----------------------------------------------------------+ |``FILES`` |一个类字典对象,包含所有上传的文件。 | | |``FILES`` 的键来自 ``
`` | | |中的 ``name`` 。 ``FILES`` 的值是一个标准的Python字典, | | |包含以下三个键: | | | | | |* ``filename`` :字符串,表示上传文件的文件名。 | | | | | |* ``content-type`` :上传文件的内容类型。 | | | | | |* ``content`` :上传文件的原始内容。 | | | | | |注意 ``FILES`` 只在请求的方法是 ``POST`` ,并且提交的 | | |``
`` 包含 ``enctype="multipart/form-data"`` 时 | | |才包含数据。否则, ``FILES`` 只是一个空的类字典对象。 | +-----------------+-----------------------------------------------------------+ |``META`` |一个标准的Python字典,包含所有有效的HTTP头信息。 | | |有效的头信息与客户端和服务器有关。 | | |这里有几个例子: | | | | | |* ``CONTENT_LENGTH`` | | | | | |* ``CONTENT_TYPE`` | | | | | |* ``QUERY_STRING`` :未解析的原始请求字符串。 | | | | | |* ``REMOTE_ADDR`` :客户端IP地址。 | | | | | |* ``REMOTE_HOST`` :客户端主机名。 | | | | | |* ``SERVER_NAME`` :服务器主机名。 | | | | | |* ``SERVER_PORT`` :服务器端口号。 | | | | | |在 ``META`` 中有效的任一HTTP头信息都是带有 ``HTTP_`` 前缀的| | |键,例如: | | | | | |* ``HTTP_ACCEPT_ENCODING`` | | | | | |* ``HTTP_ACCEPT_LANGUAGE`` | | | | | |* ``HTTP_HOST`` :客户端发送的 ``Host`` 头信息。 | | | | | |* ``HTTP_REFERER`` :被指向的页面,如果存在的。 | | | | | |* ``HTTP_USER_AGENT`` :客户端的user-agent字符串。 | | | | | |* ``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的原始数据。 用于对数据的复杂处理。 | +-----------------+-----------------------------------------------------------+
备注:
译者: