I have to +1 Carstens answer.
In short, I too find CB views to be too "magical".
Post by Carsten FuchsHi Andrew,
many thanks for your clear and detailed explanation!
Having used Django for several years, FVs are still my favorite approach
over CBVs and GCBVs. In the past, I made several starts with CBVs, but
never found or understood why CBVs and GCBVs seem to be in many people's
center of attention.
FVs are not only the most simple of the three, but they also have the
virtue of emphasizing the basic generic HTML web functionality and the
way how forms work: The three logical paths that form processing can
take are clearly shown in e.g. this (modified) example from
https://django-book.readthedocs.io/en/latest/chapter07.html
form = ContactForm(request.POST)
# success, now use form.cleaned_data
return HttpResponseRedirect('/contact/thanks/')
form = ContactForm()
# We get here both for GET requests and POST requests
# with invalid forms.
return render(request, 'contact_form.html', {'form': form})
When I was new to Django (and in fact new to serious web development),
understanding this concept, as step by step explained in the above
mentioned chapter, was one of the key insights for me.
CBV and GCBV obscure this imho fundamental concept. Personally, I've
never found the CBV's help with DRY outweigh the clarity of FVs. With
all the attention and efforts that are spent on making getting started
with Django easier for beginners, I was really surprised when even the
official Django tutorials were changed from FVs to CBVs.
I write this email to thank you for your explanation, but also to add
the above thoughts, which I have long been pondering but never found a
place to write down. ;-)
Best regards,
Carsten
Post by Andrew Pinkham- 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.
Post by Andrew PinkhamLet'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"])
"""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
"""Demonstrate HTTP Request/Response"""
"""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.
Post by Andrew PinkhamStated otherwise: FV and CBV are *equivalent* with the exception of
automatic OPTIONS handling in CBV.
Post by Andrew PinkhamGCBV 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 .
Post by Andrew PinkhamSo, 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.
Post by Andrew PinkhamSo 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.
Post by Andrew PinkhamHowever, 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!).
Post by Andrew PinkhamIf 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).
Post by Andrew PinkhamHope 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
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/3e4ce109-2f24-edfa-0eb3-3426cc52816d%40cafu.de
.
For more options, visit https://groups.google.com/d/optout.