ricardomol
  • Ricardomol Notes
  • Frontend
    • Javascript Toolchain
    • Javascript
      • Quirks
      • Articles
        • Function caching
        • 12 JS Tricks
      • Closures
      • Assorted
      • ES6
      • this
      • OOP
      • Async Programming
      • Functional Programming
      • Typescript
    • React
      • Patterns
        • Render props
      • React Router
    • Webpack
    • CSS
      • Resources
  • Backend
    • Python
      • Shallow copy vs deep copy
      • Classes
      • Resources
      • Python C Extensions
      • Coroutines
      • Exceptions
      • Context managers
      • One-Liners
      • Open function
      • Object introspection
      • Targeting Python 2 + 3
      • For - else
      • Comprehensions
      • Lambdas
      • __slots__ magic
      • Collections
      • Enumerate
      • Mutation
      • Map, Filter and Reduce
      • Decorators
      • Sets
      • Fluent Python summary
      • Quizes / Tips
      • Generators
    • Django
      • Generic Relations
      • FBV's vs CBV's
      • ORM
      • DRF
    • RESTful Architecture
    • Resources
  • Databases
    • Joins
    • Normalization
    • PostgreSQL
  • DevOps
    • Docker
      • 0. Resources
      • 2. Services
      • 3. Swarms
      • 5. Stacks
      • 6. Deploy your app
    • CI
      • CI with Django
    • CD
    • PaaS
    • WSGI servers
    • Django
      • Django Deployment
    • Modern DevOps with Django
  • Git
    • Git
  • Comp Sci
    • Big O Notation
    • Patterns
    • Programming paradigms
  • Assorted
    • TCP vs UDP
    • Tests
    • MongoDB
    • Node
      • Resources
    • Go
    • HTTP vs HTTP2
    • GraphQL
    • Books
    • Vim
    • IPv4 vs IPv6
    • Regex
    • Redis
    • Celery
      • Brokers
    • Caching
  • SECURITY
    • Security
Powered by GitBook
On this page
  • Function-Based Views vs Class-Based Views
  • Function-based views (FBV)
  • Class-based Views (CBVs)
  • Generic Class-based Views (GCBVs)
  • The winner?
  1. Backend
  2. Django

FBV's vs CBV's

Source: https://wsvincent.com/class-function-based-views/

PreviousGeneric RelationsNextORM

Last updated 6 years ago

Function-Based Views vs Class-Based Views

Within Django a views.py file adds logic to HTTP requests and responses. If we attempt to visualize how a Django webpage is displayed the process goes something like this:

  • user enters in a URL like example.com

  • a urls.py file identifies the proper URL route

  • the views.py file associated with that URL route handles the HTTP request, specifies the proper template, database model (if needed), and logic necessary, and then sends back an HTTP response to the user

This process happens over and over again for each webpage within a Django site.

Function-based views (FBV)

Historically Django used only function-based views (FBV). These take a standard Python function form of accepting a request object as an argument and returning a response object.

Here is an example of a function that returns a simple template. For simplicity, no database models are included here.

# helloworld/views.py
from django.shortcuts import render

def homepageView(request):
    return render(request, 'helloworld/index.html')

Notice that the view accepts a single request object as the argument and uses the built-in shortcut to return a response. In this case, it displays the template index.html within the helloworld app.

Upside: there is no hidden behavior; you know exactly what you’re getting. Decorators can be added, yes, but largely what you see is what you get.

Downside: length and repetition.

Class-based Views (CBVs)

What’s the cost? You need to understand the underlying inheritance structure well before you start making changes.

Generic Class-based Views (GCBVs)

The winner?

Personally I always start with a GCBV if possible, drop down to a CBV if needed, and if I really must get granular with behavior I’ll use a FBV on occasion.

Starting with class-based views were added and by leveraging inheritance, views code became much more concise. Also customizing an existing CBV is straight-forward; only the parts that have changed need to be updated. Goodbye repetition. And goodbye long, hard-to-read views.

Django took this a step further with Generic Class-based Views (GCBVs) which handle common use cases such as , displaying content in , or as an .

GCBVs are wonderfully concise and powerful. However they can be hard to customize since that requires diving into their internals and really understanding the underlying methods and inheritance structure. is a wonderful resource for unpacking views in this way.

It’s worth noting that the uses CBVs and via more and more behavior that in the past required a FBV is becoming available within a CBV context.

render
Django 1.3
displaying templates
list form
individual detail
Classy Class-Based Views
Django source code
mixins