masterpiece
masterpiece

Reputation: 753

What is the HttpRequest metadata that's passed to a django view function

The django documentation states:

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.

Example:

from django.http import HttpResponse
import datetime

def current_datetime(request):
    now = datetime.datetime.now()
    html = "<html><body>It is now %s.</body></html>" % now
    return HttpResponse(html)

Each view function takes an HttpRequest object as its first parameter, which is typically named request.

What kind of metadata does the request argument holds and gets passed to the view function when called?

Upvotes: 0

Views: 2291

Answers (1)

Aidan Ewen
Aidan Ewen

Reputation: 13328

Have a look at the docs.

It contains python representations for various attributes of an http request.

examples -

request.path # the url (excluding domain)
request.method # eg GET or POST
request.cookies
request.user # A django.contrib.auth.models.User object representing the currently logged-in user
request.META # A standard Python dictionary containing all available HTTP headers

Upvotes: 1

Related Questions