Advanced Django
Class Based Views
Let’s learn something!
Django Bootcamp
● Welcome to your first “Advanced” Django
Section.
● In this section we will discuss Class
Based Views “CBV”
Django
● Previously we’ve created views using
functions, however Django provides
really powerful tools to use OOP and
classes to define views.
Django
● The CBV offers great functionality and for
most experienced users of Django, it is
their default choice for creating views.
● It is much easier to understand CBV after
working with function views!
Django
● We’ll start off with a simple example of a
“Hello World” CBV and then slowly build
up to more complex examples and
talking about “mix-ins”.
● Let’s get started!
CBV - Hello World
Let’s learn something!
Django
● Let’s convert a simple hello world
function view into a class based view!
● We will use the simplest available Django
View Class:
○ from django.views.generic import View
Django
● We will also have to slightly change the
way we call a class based view in the
urls.py file of our project.
● We need to add in a .as_view() call off the
class, this is an inherited method from
the View we mentioned earlier.
CBV - Template Views
Let’s learn something!
Django
● Now it is time to learn how to use the
TemplateView that comes with Django.
● It will make calling templates a breeze!
● Let’s see a comparison between using a
function to call a template versus the
TemplateView!
Django
● Function Based View
def index(request):
return render(request,'index.html')
● Class Based Template View
class IndexView(TemplateView):
template_name = 'index.html'
Django
● Let’s show the basics of this and then
show how to use a Template CBV along
with a context dictionary.
List Views
and Detail Views
Let’s learn something!
Django
● We’ve learned how to use CBVs to
directly show a template, but what about
models?
● Often when we have models, we want to
either list the records from the model, or
show details of a single record.
Django
● Previously we did this with calls using
the Object Relation Mapper directly
● This included things like:
○ MyModel.objects.all()
● However these sort of operations are very
common!
Django
● So common that Django has some
generic view classes you can inherit to
very quickly display information from
your model.
● This is where the power of CBV comes to
help us out!
Django
● In this lecture we will quickly create:
○ New Models
○ New Templates
● Then we will focus on:
○ ListView
○ DetailView
Django
● Previously we’ve been putting all our
templates inside the templates folder
within the matching app folder.
● However it is also common practice to do
the “reverse”, have a template folder
inside the app’s folder.
Django
● We’ll show an example of doing that in
this lecture as well.
● Let’s get started!
CRUD
Let’s learn something!
Django
● You may have heard the term “CRUD”
before in web development, but what
does it actually mean?
● Contrary to what you might think, it
stands for Create Retrieve Update Delete
● CRUD is inherent to almost every
website!
Django
● Whenever you work with models and
databases you will need to perform those
four basic actions.
● Luckily, Django has class based views to
simplify this entire process for you!
Django
● We’ll start off by exploring how to use the
CreateView class.
● Note! While we are using the CreateView
class we will purposefully induce a few
errors to clarify where certain variable
names are coming from!
Django
● Once we’ve worked with the CreateView
class, working with the UpdateView and
DeleteView classes will be very
straightforward.
Django
● A quick note: there will be a lot of
interaction between your urls.py,
views.py, models.py, and template files!
● If you get stuck on an error triple-check
that your code matches the notes
exactly!
Django
● The nature of the interaction between all
the files will make it almost impossible to
give good help on this in the QA!
● So follow along with the video, and check
against the notes!
● Let’s get started!