django class based views for beginners

Post on 15-Apr-2017

533 Views

Category:

Software

19 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Django Class-based Viewsfor Beginners

Spin Lai

Function-based View

Function-based View

What is Function-based View?

▸ A Django views is a function that :

▸ Take an HTTP request as the input

▸ Turn it into a HTTP response

▸ a.k.a Function-based Views (FBVs)

Function-based View

View Functions

Function-based View

View Functions

Function-based View

How does it work?

Request

Django FBV

post_list() Response

Class-based View

Class-based View

What is Class-based View?

▸ Django views based on class

▸ Actually called as Function

▸ Using mixins to add functionality

Class-based View

What does it look like?

Class-based View

How does it work?

Request

Django CBV

as_view()

dispatch()

get() / post() / put() …

Response

Mixins

Mixins

Image source : http://atwinkyaday.blogspot.tw/2012_12_01_archive.html

Mixins

Flavour

Image source : http://thebakingbird.com/ice-cream/humphry-slocombe-the-hipster-ice-cream-parlor-of-ice-cream-parlors/

Mixins

+Flavour

Mix-insImage source : http://coldstoneofaberdeen.com/ice-cream/mix-go/

Mixins

+ =Flavour

Mix-insImage source : http://www.thecomfortofcooking.com/2014/04/amazing-no-churn-ice-cream-6-flavors.html

Mixins

What is Mixin in Python?

▸ Actually, mixins are ordinary Python classes

▸ Provides functionality to be inherited

▸ But isn’t meant to be instantiated on its own

Mixins

Why?

▸ Adding functionality to classes

▸ Improve modularity

Mixins

When to use?

▸ Reuse code / features across multiple classes

Mixins

Rule of thumb

▸ Base view classes provided by Django always go to the right.

▸ Mixins go to the left of the base view.

▸ Mixins should inherit from Python’s built-in object type

▸ Keep shallow inheritance chain

Mixins - Rule of thumb

Base view classes provided by Django always go to the right

Mixins - Rule of thumb

Mixins go to the left of the base view

Mixins - Rule of thumb

Mixins should inherit from Python’s built-in object type

Built-in Class-based Generic Views

Built-in Class-based Generic Views

Category of Built-in Class-based Generic Views

▸ Base

▸ List

▸ Detail

▸ Edit

Built-in Class-based Generic Views

Base Generic Views

▸ django.views.generic.View

▸ django.views.generic.TemplateView

▸ django.views.generic.RedirectView

Built-in Class-based Generic Views

List Generic Views

▸ django.views.generic.list.ListView

Built-in Class-based Generic Views

Detail Generic Views

▸ django.views.generic.detail.DetailView

Built-in Class-based Generic Views

Edit Generic Views

▸ django.views.generic.FormView

▸ django.views.generic.CreateView

▸ django.views.generic.UpdateView

▸ django.views.generic.DeleteView

Usage of CBVs

Usage of CBVs

TemplateView

Usage of CBVs

ListView

Usage of CBVs

CreateView

CBVs and Mixins

CBVs and Mixins

TemplateView

TemplateView

View ContextMixin TemplateResponseMixin

CBVs and Mixins

ListView

ListView

View TemplateResponseMixin

ContextMixin

MultipleObjectMixin

BaseListView MutipleObjectTemplateResponseMixin

CBVs and Mixins

DetailView

DetailView

View TemplateResponseMixin

ContextMixin

SingleObjectMixin

BaseDetailView SingleObjectTemplateResponseMixin

CBVs and Mixins

FormView

FormView

ProcessFormView

TemplateResponseMixin

ContextMixin

FormMixin

BaseFormView

View

The Dark Side

The Dark Side

The Dark Side of Mixins

▸ Easy to lose track of the origin of your methods

▸ You may actually polluting the class namespace

The Dark Side

The Dark Side of Generic CBVs

▸ Flow control is totally hidden

▸ The order of execution may not be obvious to anyone else

▸ More difficult to debug

▸ To understand what’s going on. You have to read the API docs, or even the source code of CBVs …

▸ Remember? “Explicit is better than implicit”

Demo

Takeaways

Takeaways

FBVs or CBVs ?

▸ Does the Generic CBVs closely match what you need?

▸ Can you use a CBV just by overriding some attributes?

▸ Do you need to subclass your views to create some views?

Takeaways

Guidelines

▸ Keep your view simple

▸ Never repeat code in your views

▸ Only handle presentation logic in views

▸ Keep your mixins simpler

▸ Don’t use CBVs to write 403, 404… error handlers. Use FBVs instead

Resources

Resources

Links

▸ https://docs.djangoproject.com/ja/1.9/topics/class-based-views/

▸ https://ccbv.co.uk/

▸ http://django-vanilla-views.org/

Resources

Books

▸ 「Two Scoops of Django: Best Practices for Django 1.8」

▸ Chap 8 Function-and-Class-Based Views

▸ Chap 9 Best Practices for Function-Based Views

▸ Chap 10 Best Practices for Class-Based Views

Thank you

top related