› Forums › Web Development › HarvardX: CS50W – CS50’s Web Programming with Python and JavaScript › CS50W – Lecture 3 – Django › Understanding Why Django Calls `index(request)` a View Even Though the Function Name Is `index`
A beginner learning Django may see code like this:
def index(request):
return HttpResponse("Hello")
and wonder:
index, why does Django call it a view?view?The important thing to understand is that:
index is the actual function nameSo in this code:
def index(request):
index → function namerequest → parameterDjango considers this function a view because it handles a web request and returns a response.
For example:
def hello(request):
return HttpResponse("Hello")
Even though the function name is hello, Django still calls it a view.
Similarly:
def homepage(request):
or:
def mypage(request):
would also be Django views.
The word “view” describes what the function does, not what it is named.
A simple analogy is:
Likewise:
index → function nameThe name index is commonly used for a main page or listing page.
Examples:
index → homepage or listing pagedetail → show one itemsearch → search pageedit → edit pageSo Django saying “index is a view” simply means:
The function named
indexis being used as a Django view function.
