Code


Version 7 (modified by Gulopine, 6 years ago) (diff)

Corrected which type of error is raised for non-existing keys

HttpRequest

Location: django.http

Represents an incoming HTTP request, including all HTTP headers and user-submitted data. For information, see the documentation.

Attributes

Unless otherwise noted, all attributes should be treated as read-only. Additional attributes may be added by middleware, as defined in a project's settings. Some attributes commonly added by middleware are listed here as well, with a link to the middleware documentation.

path String containing the full path (not including domain) of the URL requested
method String containing the HTTP method used by the request ('GET' or 'POST')
GET A QueryDict representing form values submitted using the HTTP GET method
POST A QueryDict representing form values submitted using the HTTP POST method
raw_post_data Unprocessed text of the complete POST data, only used in advanced cases, where POST data cannot be parsed into a QueryDict
COOKIES A dictionary of cookies sent by the browser during the request
META A dictionary of included HTTP headers; available headers will vary based on client and server
user A User (or AnonymousUser) object from the authentication middleware
session A special read-write dictionary provided by the sessions middleware

Methods

build_absolute_uri(location) Returns an absolute URI for the given location, whether that be relative or absolute
has_key(key) Returns True if the given key was provided in the request (GET or POST). False otherwise
get_full_path() Returns the full request string, including any query string that was provided
is_secure() Returns True if the request was made using HTTPS, False otherwise

Dictionary Syntax

HttpRequest objects allow form data to be retrieved using standard Python dictionary syntax. It will check for GET data first, then POST. Like any dictionary, accessing a non-existant key will result in a KeyError. This access is read-only.

username = request['username']