Junior — Middle
Explain the working principle of the FBV function in the Django framework
sobes.tech AI
Answer from AI
FBV (Function-Based View) in Django is a way of creating HTTP request handlers as regular functions. Each such function takes a request object (HttpRequest) and returns a response object (HttpResponse).
Working principle:
- When a request arrives, the URL is matched to a function via routing (urls.py).
- The function processes the request, performs logic (for example, accessing a database, forming data).
- It returns a response, which can be an HTML page, JSON, redirect, etc.
Example of FBV:
from django.http import HttpResponse
def hello_view(request):
return HttpResponse("Hello, world!")
FBVs are simple and convenient for small tasks, but as logic complexity increases, they often transition to Class-Based Views (CBV).