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'),
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With