0
0
Djangoframework~10 mins

ViewSets and routers in Django - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - ViewSets and routers
Client sends HTTP request
Router matches URL to ViewSet
ViewSet selects action based on HTTP method
ViewSet calls appropriate method (list, create, retrieve, update, destroy)
ViewSet returns HTTP response
Client receives response
The router receives the request URL and directs it to the ViewSet, which picks the right method based on HTTP method and returns the response.
Execution Sample
Django
from rest_framework import viewsets, routers
from rest_framework.response import Response

class BookViewSet(viewsets.ViewSet):
    def list(self, request):
        return Response(['Book1', 'Book2'])
A simple ViewSet with a list method returning a list of books.
Execution Table
StepRequest URLHTTP MethodRouter ActionViewSet Method CalledResponse
1/books/GETMatches 'books' routelist['Book1', 'Book2']
2/books/1/GETMatches 'books' route with pk=1retrieve405 Method Not Allowed (method not implemented)
3/books/POSTMatches 'books' routecreate405 Method Not Allowed (method not implemented)
4/authors/GETNo matching routeNone404 Not Found
💡 Execution stops when response is returned or no matching route is found.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4
request.url/books//books/1//books//authors/
request.methodGETGETPOSTGET
viewset_method_calledlistretrievecreateNone
response['Book1', 'Book2']405 Method Not Allowed405 Method Not Allowed404 Not Found
Key Moments - 3 Insights
Why does a GET request to /books/1/ return 405 Method Not Allowed?
Because the retrieve method is not implemented in the ViewSet, so the router calls it but it returns 405 as default. See execution_table row 2.
Why does a POST request to /books/ return 405 Method Not Allowed?
Because the create method is not implemented in the ViewSet, so the router calls it but it returns 405 by default. See execution_table row 3.
What happens if the URL does not match any route?
The router cannot find a matching route and returns 404 Not Found immediately. See execution_table row 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what method does the ViewSet call for a GET request to /books/?
Aretrieve
Blist
Ccreate
Dupdate
💡 Hint
Check the 'ViewSet Method Called' column for Step 1.
At which step does the router fail to find a matching route?
AStep 1
BStep 2
CStep 4
DStep 3
💡 Hint
Look at the 'Router Action' column for each step.
If the create method was added to the ViewSet, what would change at Step 3?
AResponse would be whatever create returns
BResponse would be 404 Not Found
CResponse would be 405 Method Not Allowed
DRouter would not match the URL
💡 Hint
See what happens when a method is implemented in the ViewSet in the execution_table.
Concept Snapshot
ViewSets group related views for a resource.
Routers map URLs to ViewSet methods automatically.
HTTP methods (GET, POST, etc.) select ViewSet actions.
Implement methods like list(), create(), retrieve() in ViewSet.
Router handles URL matching and calls the right method.
Unimplemented methods return 404 or 405 errors.
Full Transcript
In Django REST Framework, ViewSets let you group related views for a resource like books. Routers automatically match URLs to these ViewSets. When a client sends a request, the router checks the URL and HTTP method, then calls the matching method on the ViewSet, such as list for GET /books/. If a method like retrieve or create is not implemented, the router returns 404 or 405 errors. This flow helps organize code and reduces URL configuration.