HttpRequest attributes

By: Priya Philip 1 year ago

Django uses request and response objects to pass state through the system. When a page is requested, Django creates an HttpRequest object that contains metadata about the request. Then Django loads the appropriate view, passing the HttpRequest as the first argument to the view function. Each view is responsible for returning an HttpResponse object.


HttpRequest represents a single HTTP request from some user-agent. All attributes except session should be considered read-only.


All attributes should be considered read-only, unless stated otherwise.

Representing the scheme of the request (http or https usually).

Useful for processing data in different ways than conventional 

HTML forms: binary images, XML payload etc.


Representing the full path to the requested page,not including the scheme or domain. 

Example, "/music/bands/the_beatles/".


Contains the path info portion of the path, no matter what Web server is being used.

It can make your code easier to move between test and deployment servers.


Representing the HTTP method used in the request.

This is guaranteed to be uppercaseExample:

if request.method == 'GET':
elif request.method == 'POST':

Representing the current encoding used to decode form submission data.

Useful if you know the form data is not in the DEFAULT_CHARSET encoding.

Representing the MIME type of the request,parsed from the CONTENT_TYPE header.
A dictionary of key/value parameters included in the CONTENT_TYPE  header.
A dictionary-like object containing all given HTTP GET parameters.

A dictionary-like object containing all given HTTP POST parameters, providing that the request contains form data.

If you need to access raw or non-form data posted in the request, access this through the HttpRequest.body attribute instead.

POST does not include file-upload information.

A dictionary containing all cookies. Keys and values are strings.


A dictionary-like object containing all uploaded files.

Each key in FILES is the name from the <inputtype="file" name="">.

Each value in FILES is an UploadedFile.


A standard Python dictionary containing all available HTTP headers.Examples:

  • CONTENT_LENGTH – the length of the request body (as a string).
  • CONTENT_TYPE – the MIME type of the request body.
  • HTTP_ACCEPT_ENCODING – Acceptable encodings for the response.
  • HTTP_ACCEPT_LANGUAGE – Acceptable languages for the response.
  • HTTP_HOST – The HTTP Host header sent by the client.
  • HTTP_REFERER – The referring page, if any.
  • HTTP_USER_AGENT – The client’s user-agent string.
  • QUERY_STRING – The query string, as a single (unparsed) string.
  • REMOTE_ADDR – The IP address of the client.
  • REMOTE_HOST – The hostname of the client.
  • REMOTE_USER – The user authenticated by the Web server, if any.
  • REQUEST_METHOD – A string such as “GET” or “POST”.
  • SERVER_NAME – The hostname of the server.
  • SERVER_PORT – The port of the server (as a string).

An object of type AUTH_USER_MODEL representing the currently logged-in user.

If the user isn’t 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.
    # Do something for anonymous users.

user is only available if your Django installation has the AuthenticationMiddleware activated.

A readable-and-writable, dictionary-like object used to  represent the current session.

Only available if your Django installation has session support activated.


Not defined by Django itself, but will be read if other code (e.g., a custom middleware class) sets it.

When present, this will be used as the root URLconf for the current request, overriding the ROOT_URLCONF setting.


An instance of ResolverMatch representing the resolved url.

This attribute is only set after url resolving took place.

It’s available in all views but not in middleware methods which are executed before url resolving takes place (like process_request, you can use process_viewinstead).



Let us talk!

We take the vision which comes from dreams and apply the magic of science and mathematics, adding the heritage of our profession and our knowledge to create a design.