Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why view functions require a request parameter In Django?

Tags:

python

django

Example from Django documentation:

def index(request):
    return HttpResponse('<h1>hellworld!</h1>')

def detail(request, question_id):
    return HttpResponse("Question: %s" % question_id)

Since the request argument is never used, why should I include it in each function signature?

like image 813
Michal Rosenbaum Avatar asked Sep 23 '17 18:09

Michal Rosenbaum


People also ask

What is request in views in Django?

Request came from User that want to load page. 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.

How does View work in Django?

In the Django framework, views are Python functions or classes that receive a web request and return a web response. The response can be a simple HTTP response, an HTML template response, or an HTTP redirect response that redirects a user to another page.

What kind of data is passed into a view in the request object?

The request object has view input field values in name/value pairs. When we create a submit button then the request type POST is created and calls the POST method. We have four data, those are in Name-Value pairs.

What is request user in Django?

user is User model object. You cannot access request object in template if you do not pass request explicitly. If you want access user object from template, you should pass it to template or use RequestContext. It depends upon what you set .


2 Answers

The purpose of a web server is to reply to HTTP requests (In simple terms). Django being a web framework generates responses depending on the request that it receives.

The business logic of handling the requests is done by Django Views, it is their purpose. This is why Django supplies the request to the view function, so the code can access the request data and choose what actions it should take and which response it should send back (even though that specific view doesn't make use of it).

The decision of requiring the view to receive the request as an argument is an implementation decision made by Django developers, making it part of the interface the view has with the rest of the "system". Other frameworks might choose making it available globally and not suplying it to the views/controllers directly, but the merits of one or another approach is other discussion.

like image 70
dethos Avatar answered Oct 13 '22 08:10

dethos


Many of the examples in Django's documentation (and any documentation, really) are necessarily contrived. They're simple because the complexity of a real example would take away from the point that's being made.

In real use cases, you'll frequently want access to the request object. You may want to know which User is attached to the request (request.user) or whether the request is a GET or a POST (request.method).

You can also inspect the headers (request.headers), decode the JSON object sent with a POST request (json.loads(request.body)), etc.

like image 36
James Brewer Avatar answered Oct 13 '22 09:10

James Brewer