← Back to list

Day 03: Understanding Django Views: Function-based Views, Class-based Views, APIView, and ViewSets…

Function-Based Views (FBVs)

Alen Sabu · 2024-05-20 15:33 · 0 claps · 1.7 min read
#django #class-based-views #function-based-views #viewset #apiview
Open on Medium ↗
Wiki topics: 🌐 · Web Development

Day 03: Understanding Django Views: Function-based Views, Class-based Views, APIView, and ViewSets with Routers

Function-Based Views (FBVs)

Function based views : views that are defined as Python functions. These functions take a web request and return a web response.

Benefits of Function-Based Views:

  1. Function based views are straightforward and easy to understand
  2. The logic for handling the request is all in one place
  3. We can freely write any Python code inside the view function

Drawbacks of Function-Based Views:

  1. Reusing common patterns and logic can lead to repetitive code
  2. As the application grows, Function based views can become hard to maintain due to their length and complexity

Class-Based Views (CBVs)

Class Based Views: allows us to define views as Python classes instead of functions. Django provides a variety of built-in Class Based Views to make any task easier

Benefits of Class Based Views:

  1. Common functionality can be easily reused through inheritance and mixins.
  2. Separating HTTP methods (like GET, POST) into separtate methods makes the code cleaner and easier to maintain.
  3. Class Based Views are highly extendable, allowing you to override or extend behavior as needed

Drawbacks of Class Based Views:

  1. Class Based Views can be harder to understand intially, especially for beginners
  2. Sometimes Class Based Views can introduce unnecessary complexity for simple views.

APIView

APIView is a class based view provided by Django Rest Framework for building APIs. It extends Django’s View class and provides additional functionality specific to API development

Benefits of APIView:

  1. DRF Integration: Comes with built-in features for parsing request data, handling authentication and rendering responses.
  2. Allows full control over the request and response handling

Use Cases:

  1. When we need full control over request and response handling
  2. When building custom API logic that doesn’t fit into the built-in views provided by DRF

ViewSets and Routers

ViewSets

ViewSets in DRF combine the logic for handling a set of related views into a single class. They abstract common patterns used in API views and reduce the amount of code to write.

Benefits of ViewSets:

  1. Reduce boilerplate code for common operations like listing, creating, updating, and deleting objects
  2. Ensure a consistent interface for your API endpoints

Routers

Routers in DRF automatically generate URL patterns for ViewSets. They help in linking ViewSets with URLs without writing explicit URL configurations

Benefits of Routers:

  1. Automatic URL Routing
  2. DRY Principle: Keeps your codebase DRY(Don’t Repeat Yourself) by reducing repetitive URL configuration.

Choosing between function-based views and class-based views depends on the specific needs of the project. FBVs are great for simple, straightforward views, while CBVs offer better reusability and organization for more complex views.

Happy Coding….


메타데이터
post_id
6fbc0151dddb
slug
day-03-understanding-django-views-function-based-views-class-based-views-apiview-and-viewsets-6fbc0151dddb
url
https://medium.com/@alensabu12xtz/day-03-understanding-django-views-function-based-views-class-based-views-apiview-and-viewsets-6fbc0151dddb
canonical_url
https://medium.com/@alensabu12xtz/day-03-understanding-django-views-function-based-views-class-based-views-apiview-and-viewsets-6fbc0151dddb
author_url
https://medium.com/@alensabu12xtz
status
ok
fetched_at
2026-07-13 08:05:13