0
0
Djangoframework~10 mins

Function-based vs class-based decision in Django - Visual Side-by-Side Comparison

Choose your learning style9 modes available
Concept Flow - Function-based vs class-based decision
Start Request
Check View Type
Function
Call function
Execute code
Return Response
End
The request first checks if the view is function-based or class-based, then calls the function or instantiates the class and calls its method, finally returning a response.
Execution Sample
Django
from django.http import HttpResponse
from django.views import View

def my_view(request):
    return HttpResponse('Hello')

class MyView(View):
    def get(self, request):
        return HttpResponse('Hello')
Shows a simple function-based view and a class-based view handling a GET request.
Execution Table
StepView TypeActionDetailsResponse
1Function-basedCall functionmy_view(request) calledNone yet
2Function-basedExecute codeReturn HttpResponse('Hello')HttpResponse with 'Hello'
3Function-basedReturn responseResponse sent to clientHttpResponse with 'Hello'
4Class-basedInstantiate classMyView() createdNone yet
5Class-basedCall methodMyView.get(request) calledNone yet
6Class-basedExecute codeReturn HttpResponse('Hello')HttpResponse with 'Hello'
7Class-basedReturn responseResponse sent to clientHttpResponse with 'Hello'
💡 Execution stops after response is returned to client.
Variable Tracker
VariableStartAfter Step 1After Step 4After Step 5Final
requestHttpRequest objectPassed to my_viewPassed to MyView instancePassed to get methodUsed to create response
responseNoneNoneNoneNoneHttpResponse('Hello')
Key Moments - 2 Insights
Why do we instantiate a class in class-based views but not in function-based views?
Because class-based views use methods inside a class, so Django creates an instance to call the method (see execution_table steps 4 and 5). Function-based views are just called directly (steps 1 and 2).
How does the request get handled differently in function vs class-based views?
In function-based views, the request is passed directly to the function. In class-based views, the request is passed to the class instance method (see variable_tracker showing request passed at different steps).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, at which step is the class instantiated in a class-based view?
AStep 5
BStep 2
CStep 4
DStep 1
💡 Hint
Check the 'Action' column for 'Instantiate class' in the execution_table.
According to the variable tracker, what is the value of 'response' after step 2 in function-based views?
AHttpResponse('Hello')
BNone
CHttpRequest object
DMyView instance
💡 Hint
Look at the 'response' row and the 'After Step 2' column in variable_tracker.
If the class-based view did not instantiate the class, what would happen?
AThe get method could not be called on an instance
BThe function would run normally
CThe response would be sent twice
DThe request would be ignored
💡 Hint
Refer to key_moments explaining why instantiation is needed before calling methods.
Concept Snapshot
Function-based views are simple functions called directly with the request.
Class-based views are classes instantiated by Django, then their methods handle requests.
Function views are straightforward; class views allow reuse and organization.
Both return HttpResponse objects to send back to the client.
Class-based views require instantiation before method calls.
Choose function views for simplicity, class views for structure.
Full Transcript
This visual execution compares function-based and class-based views in Django. When a request comes in, Django checks if the view is a function or a class. For function-based views, it calls the function directly with the request, executes the code, and returns a response. For class-based views, Django first creates an instance of the view class, then calls the appropriate method (like get) with the request. Both approaches end by returning an HttpResponse to the client. The variable tracker shows how the request and response variables change during execution. Key moments clarify why class instantiation is necessary and how request handling differs. The quiz tests understanding of these steps and concepts.