• ↑↓ pour naviguer
  • pour ouvrir
  • pour sélectionner
  • ⌘ ⌥ ↵ pour ouvrir dans un panneau
  • ←→ pour naviguer
  • esc pour rejeter
⌘ '
raccourcis clavier

Formal Definition

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.

Explanation

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.

How It Works

  1. URL pattern matches — URL dispatcher resolves path to view function
  2. Request object created — Django builds HttpRequest with GET, POST, FILES, COOKIES, session, user
  3. View function calledview_func(request, *args, **kwargs) executed
  4. Business logic runs — Query models, process forms, call services, etc.
  5. Response returnedHttpResponse, JsonResponse, render(), redirect(), or HttpResponseNotFound
  6. Middleware processes response — Response middleware modifies headers, compresses, etc.

Visual Explanation

function_based_view Request HttpRequest GET /hello/ ViewFunc def hello(request):    return HttpResponse('Hello!') Request->ViewFunc 1. Called with request Logic Business Logic (optional Model access) ViewFunc->Logic 2. Execute logic Response HttpResponse 'Hello!' ViewFunc->Response 4. Return response Logic->ViewFunc 3. Return data

Semantic Network

semantic_function_based_views THIS Function-Based Views (FBV) PRE1 URL Dispatcher THIS--PRE1 built from PRE2 HttpRequest Object THIS--PRE2 built from PRE3 HttpResponse Classes THIS--PRE3 built from OUT1 Django Forms Processing THIS--OUT1 builds into OUT2 Model CRUD Operations THIS--OUT2 builds into OUT3 Template Rendering THIS--OUT3 builds into OUT4 JSON API Endpoints THIS--OUT4 builds into CON1 Class-Based Views (CBV) THIS--CON1 contrasts with REL1 Decorators (@login_required) THIS--REL1 related REL2 Middleware (Request/Response) THIS--REL2 related

Key Properties

  • 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_exempt stack cleanly
  • Testability: Easy to unit test — call function with mock request, assert response
  • Flexibility: Can return any HttpResponse subclass; stream, file, JSON, redirect

Connections

Edge Cases & Gotchas

  • CSRF protection: POST forms need {% csrf_token %} or @csrf_exempt (dangerous)
  • Method safety: Forgetting to check request.method leads 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