Montréal-Django

Introduction to Class-Based Views

https://slides.com/ctrlweb/django-5/live/
http://invite.yuldev.ca/

Welcome

Daniel LeBlanc

CEO at ctrlweb

@danidou

dleblanc78

YulDev Organizer

Montréal-Django Organizer

daniel@ctrlweb.ca

CTO at Gaspard Media

Noël Rignon

TA at Maison Logiciel Libre

CTO at Joii

RignonNoel

noël-rignon-5b9172b9

rignon.noel@openmailbox.org

Written by

Presented by

Montréal-Django Presenter

Job Offers

Job Seekers

Announcements

Why use CBVs?

  • Implementing common web development tasks

  • Extensible

  • Decorating class-based views

  • Simple usage in your URLconf

  • Standardize the code

 

Advantage:

  • Many attributes

  • Many methods

  • Many heritage

 

Disadvantage:

Learning curve

As all class a CBV have:

A list of attributes: (including)

  • model
  • template_name
  • context_object_name

 

A list of methods / behaviors: (including)

  • dispatch()
  • get()
  • post()

Class presented

Edit :

  • CreateView
  • UpdateView
  • DeleteView

List :

  • ListView

Base :

  • RedirectView
  • TemplateView

Detail :

  • DetailView

Resource:

CreateView

View for creating a new object instance.

The HTTP GET method return a form.

The HTTP POST method create the object.
from django.views.generic import CreateView

UpdateView

View for updating an object.

The HTTP GET method return a form.

The HTTP POST method update the object.
from django.views.generic import UpdateView

DeleteView

View for deleting an object retrieved with `self.get_object()`.

The HTTP GET method return a template of validation.

The HTTP POST method delete the object.
from django.views.generic import DeleteView

ListView

Render some list of objects, set by :

 - `self.model`
or
 - `self.queryset`

Need to be any iterable of items, not just a queryset.
from django.views.generic import ListView

DetailView

Render a "detail" view of an object.

By default this is a model instance looked up from `self.queryset`, but the
view will support display of any object by overriding `self.get_object()`.
from django.views.generic import DetailView

RedirectView

A view that provides a redirect on any GET request.
from django.views.generic import RedirectView

TemplateView

A view that renders a template.  

This view will also pass into the context
any keyword arguments passed by the URLconf.
from django.views.generic import TemplateView

Merci!

Thank you!

Introduction to Class Based Views

By Daniel LeBlanc

Introduction to Class Based Views

Montréal Django #5

  • 714