A Function-Based View (FBV) is a Python callable that accepts an HttpRequest object as its first parameter and returns an HttpResponse object, implementing request-handling logic directly in a function body with explicit control over HTTP methods, status codes, and response content.
FBVs are Django’s original and most direct view pattern — a plain Python function that receives a request and returns a response. They provide complete control over the request/response cycle, making them ideal for simple endpoints, custom logic that doesn’t fit generic patterns, and developers who prefer explicit over implicit behavior. Each HTTP method (GET, POST, etc.) is handled with explicit if request.method == 'POST': checks.
- URL pattern matches — URL dispatcher resolves path to view function
- Request object created — Django builds
HttpRequestwithGET,POST,FILES,COOKIES,session,user - View function called —
view_func(request, *args, **kwargs)executed - Business logic runs — Query models, process forms, call services, etc.
- Response returned —
HttpResponse,JsonResponse,render(),redirect(), orHttpResponseNotFound - Middleware processes response — Response middleware modifies headers, compresses, etc.
- Explicit control: Every line of logic visible; no hidden inheritance chains
- Method handling: Manual
if request.method == 'POST':branching - Decorator composition:
@require_http_methods,@login_required,@csrf_exemptstack cleanly - Testability: Easy to unit test — call function with mock request, assert response
- Flexibility: Can return any
HttpResponsesubclass; stream, file, JSON, redirect
- Built from: URL Dispatcher — Receives matched requests
- Built from: HttpRequest Object — Input parameter
- Built from: HttpResponse Classes — Return types
- Builds into: ModelForms Processing — Handle form submission
- Builds into: Model CRUD Operations — Create/read/update/delete
- Builds into: Template Rendering —
render(request, template, context) - Contrasts with: Class-Based Views — Implicit behavior via inheritance
- Related: View Decorators — Cross-cutting concerns (auth, CSRF, methods)
- Related: Middleware — Global request/response processing
- CSRF protection: POST forms need
{% csrf_token %}or@csrf_exempt(dangerous) - Method safety: Forgetting to check
request.methodleads to GET-side effects - Code duplication: Similar CRUD views repeat boilerplate; CBVs/DRF reduce this
- Large functions: Complex views become hard to maintain; split into services/helpers