-->

HttpRequest attributes

By: Priya Philip 4 months, 1 week 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

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

Attributes

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

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


Useful for processing data in different ways than conventional 

HTML forms: binary images, XML payload etc.

HttpRequest.path


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

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

HttpRequest.path_info


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.

HttpRequest.method


Representing the HTTP method used in the request.

This is guaranteed to be uppercaseExample:

if request.method == 'GET':
    do_something()
elif request.method == 'POST':
    do_something_else()
HttpRequest.encoding


Representing the current encoding used to decode form submission data.

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

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


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.

HttpRequest.COOKIES
A dictionary containing all cookies. Keys and values are strings.
HttpRequest.FILES

 

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.

HttpRequest.META

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).
HttpRequest.user

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.
else:
    # Do something for anonymous users.

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

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

Only available if your Django installation has session support activated.

HttpRequest.urlconf

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.

HttpRequest.resolver_match

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).

 

Comments

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.