0
0
Djangoframework~10 mins

Function-based views basics in Django - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Function-based views basics
HTTP Request Received
URL Dispatcher Matches URL
Call Function-based View
Process Request in View Function
Return HttpResponse Object
Send Response to Client
The web server receives a request, Django matches the URL, calls the function-based view, which processes the request and returns a response.
Execution Sample
Django
from django.http import HttpResponse

def hello_view(request):
    return HttpResponse('Hello, world!')
A simple function-based view that returns a plain text 'Hello, world!' response.
Execution Table
StepActionInputOutputNotes
1Receive HTTP GET requestGET /hello/Request object createdRequest object contains method, path, headers
2URL dispatcher matches '/hello/'/hello/Calls hello_view(request)Django finds the view function for URL
3Execute hello_view functionrequest objectHttpResponse objectReturns response with content 'Hello, world!'
4Send HttpResponse to clientHttpResponse objectClient receives 'Hello, world!'Response sent back over HTTP
5EndN/AN/ARequest cycle complete
💡 Request processed and response sent, no further action.
Variable Tracker
VariableStartAfter Step 2After Step 3Final
requestN/ARequest object with GET /hello/Same request object passedSame request object
responseN/AN/AHttpResponse('Hello, world!')HttpResponse object sent to client
Key Moments - 3 Insights
Why does the view function take 'request' as a parameter?
The 'request' parameter holds all information about the client's HTTP request. In the execution_table step 3, the view uses this to process and decide what response to return.
What does the view function return?
The view must return an HttpResponse object, as shown in execution_table step 3, which Django sends back to the client.
How does Django know which function to call for a URL?
Django uses the URL dispatcher to match the URL to a view function, as seen in execution_table step 2.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the output of step 3?
ARequest object with GET method
BURL pattern string '/hello/'
CHttpResponse object with 'Hello, world!' content
DClient's browser window
💡 Hint
Check the 'Output' column in step 3 of the execution_table.
At which step does Django match the URL to the view function?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at the 'Action' column to find when URL matching happens.
If the view function returned a string instead of HttpResponse, what would happen?
ADjango would raise an error because it expects HttpResponse
BThe URL dispatcher would fail to match the URL
CDjango would send the string as response automatically
DThe client would receive an empty response
💡 Hint
Recall the 'Output' expected in step 3 of the execution_table.
Concept Snapshot
Function-based views in Django:
- Receive 'request' object as parameter
- Process request inside the function
- Return an HttpResponse object
- URL dispatcher maps URLs to view functions
- Response sent back to client after view returns
Full Transcript
In Django, when a client sends an HTTP request, the URL dispatcher matches the URL to a function-based view. This view function receives a request object containing all request details. The function processes the request and returns an HttpResponse object with the content to send back. Django then sends this response to the client, completing the request cycle. The key is that the view must accept the request parameter and return an HttpResponse. This simple flow allows Django to handle web requests efficiently.