A Django view is the code that handles a page visit. Django matches a visited URL to a view, calls it with an HTTP request, and sends the view’s response back to the browser. A view may be a function or a class; function-based views keep the first version of that journey pleasantly direct. section 1.1 section 2.5
A function-based view takes an HttpRequest argument, conventionally named request, and returns an HttpResponse or similar response object. The request holds information sent by the browser, including headers, the URL, and query parameters. URL parameters can become additional function arguments. section 1.1.1 section 2.5
Views provide the bridge between a URL and useful output:
- Render server-composed HTML by calling the template engine, often after gathering data through the ORM. section 1.2 section 1.2.1
- Return JSON data for a single-page application instead of rendering HTML. section 1.2.1
- Apply access logic before returning protected content, such as restricted downloads or pages requiring login. section 7.4.4 section 6.8
A minimal credits page
A credits(request) function can place plain-text names in a string and return HttpResponse(content, content_type="text/plain"). Registering its URL lets Django call the function when someone visits that address. It is not glamorous, but every web page has to start somewhere. section 2.5 section 2.9
For a listing page, a view can query all Musician objects, order them by last name, place the result in a context dictionary, and render a template. The template then iterates over the objects and links each one to its detail page. section 4.6.2
Listing 2.2 Credits view
# RiffMates/home/views.py
from django.http import HttpResponse #1
def credits(request): #2
content = "Nicky\nYour Name" #3
return HttpResponse(content, content_type="text/plain") #4
Inspect the smallest complete view: an imported HttpResponse, a request parameter, content, and a returned response. section 2.5
Listing 4.8 Index page for Musician objects
# RiffMates/bands/views.py
...
def musicians(request):
data = {
'musicians':Musician.objects.all().order_by('last_name'), #1
}
return render(request, "musicians.html", data)
Inspect a listing view that queries ORM data, passes it as template context, and renders an HTML response. section 4.6.2

