Hi 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
def contact(request):
if request.method == 'POST':
form = ContactForm(request.POST)
if form.is_valid():
# success, now use form.cleaned_data
return HttpResponseRedirect('/contact/thanks/')
else:
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
Am 21.11.18 um 21:39 schrieb Andrew Pinkham:
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/3e4ce109-2f24-edfa-0eb3-3426cc52816d%40cafu.de.
For more options, visit https://groups.google.com/d/optout.