Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is "request" in Django view

Tags:

python

django

In the Django tutorial for the first app in Django we have

from django.http import HttpResponse

def index(request):
    return HttpResponse("Hello, world. You're at the polls index.")

And then the urls.py has

from django.conf.urls import url

from polls import views

urlpatterns = [
    url(r'^$', views.index, name='index'),
]

Now my question is what is the "request" parameter passed to the index function, also when the function index is called in urls.py it is not passed and variables it is just called as views.index in the line url(r'^$', views.index, name='index'),

like image 892
Dr Manhattan Avatar asked Nov 27 '14 11:11

Dr Manhattan


2 Answers

The request parameter is a HttpRequest object, which contains data about the request (see the docs for django 3.2).

In your urls file, you are not calling the view.index function, just listing a reference to it. Django then calls the function when a matching request comes in and passes the HttpRequest object as a parameter.

like image 196
Matti John Avatar answered Sep 25 '22 08:09

Matti John


This doesn't directly answer your question, but I suggest you watch this video:

A Scenic Drive through the Django Request-Response Cycle

This is a PyCon talk Dan Langer gave this year and showed how request and response work under the hood.

like image 22
laike9m Avatar answered Sep 22 '22 08:09

laike9m