Django developers talk about three kinds of views: - function views (FV) - class-based views (CBV) - generic class-based views (GCBV)
People do not make always make the difference between CBV and GCBV, which is unfortunate, as they serve different purposes (naming things is hard). When Andréas states earlier in this thread that "(CBV) use a lot of defaults for populating your templates, forms and views" that is not 100% precise. He means GCBV---which provide default (generic) behavior---not CBV. Let's break it down. Below is an example of a FV. from django.http import HttpResponse from django.views.decorators.http import ( require_http_methods ) # below is equivalent to require_safe decorator @require_http_methods(["GET", "HEAD"]) def hello_world(request): """Demonstrate HTTP Request/Response""" return HttpResponse("Hello World") Below is an example of an equivalent CBV. from django.http import HttpResponse from django.views import View class HelloWorld(View): """Demonstrate HTTP Request/Response""" def get(self, request): """Handle GET HTTP method""" return HttpResponse("Hello World") Formally, a CBV is any class that inherits from View. The only difference between the two views above is that the View class being inherited will give you automatic handling of HTTP OPTIONS. Stated otherwise: FV and CBV are *equivalent* with the exception of automatic OPTIONS handling in CBV. GCBV are simply CBV that have been given behavior. For example, instead of programming a view that shows a template with model data, you can instead inherit a DetailView, and customize it by setting class variables and by overriding methods. For more about that, I recommend looking at https://ccbv.co.uk . So, when should you use a FV, CBV, or GCBV? If you are building a view that a GCBV provides behavior for, save yourself time and use it! It's easy to add or slightly modify GCBV behavior, but difficult to remove behavior. The moment you're thinking about removing something a GCBV does, stick to a function or CBV. So then, for choosing between FV or CBV: Do you need to handle multiple HTTP methods? Is there shared behavior between how the resource is handled by those HTTP methods? If yes, a CBV can help organize that logic and avoid duplicate code. However, if you have a simple view (typically only one or two HTTP methods must be handled), then a FV will serve you fine (remember the view decorators!). If you're not sure, start with a FV, and then switch to a CBV or GCBV if appropriate (as complexity goes up or when you realize you can use a GCBV). Hope that helps, Andrew https://jambonsw.com https://django-unleashed.com -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at https://groups.google.com/group/django-users. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/C2853144-5CA1-4FAD-ACDF-C487AE8CE47E%40andrewsforge.com. For more options, visit https://groups.google.com/d/optout.