[djangocon2015] demystifying mixins with django

39
Demystifying Mixins with Django

Upload: ana-balica

Post on 10-Aug-2015

610 views

Category:

Technology


3 download

TRANSCRIPT

Page 1: [Djangocon2015] Demystifying mixins with Django

Demystifying Mixins

with Django

Page 2: [Djangocon2015] Demystifying mixins with Django

@anabalica

Page 3: [Djangocon2015] Demystifying mixins with Django

Mixins area controlled

way of adding functionality to classes.

Page 4: [Djangocon2015] Demystifying mixins with Django

Mixins are notspecial language

constructs.

Page 5: [Djangocon2015] Demystifying mixins with Django

In fact, mixins are ordinary Python classes.

1 class SomeMixin(object): 2 """My smart mixin""" 3 4 def test_method(self): 5 pass

Page 6: [Djangocon2015] Demystifying mixins with Django

Why use mixins?

to improve modularity

Page 7: [Djangocon2015] Demystifying mixins with Django

When to use mixins?

want to reuse a particular feature in a lot

of different classes

Page 8: [Djangocon2015] Demystifying mixins with Django

Properties

• single responsibility• not meant to be extended• not meant to be instantiated

Page 9: [Djangocon2015] Demystifying mixins with Django
Page 10: [Djangocon2015] Demystifying mixins with Django
Page 11: [Djangocon2015] Demystifying mixins with Django

In Python the concept of mixins is implemented using

multiple inheritance.

Page 12: [Djangocon2015] Demystifying mixins with Django

Order matters

Page 13: [Djangocon2015] Demystifying mixins with Django

1 class Foo(BaseFoo, SomeMixin): 2 pass

base class mixin

Page 14: [Djangocon2015] Demystifying mixins with Django

1 class Foo(BaseFoo, SomeMixin): 2 pass

Page 15: [Djangocon2015] Demystifying mixins with Django

1 class Foo(SomeMixin, BaseFoo): 2 pass

Page 16: [Djangocon2015] Demystifying mixins with Django

1 # some_app/views.py 2 from django.views.generic import TemplateView 3 4 5 class AboutView(TemplateView): 6 template_name = "about.html"

Page 17: [Djangocon2015] Demystifying mixins with Django

1 # some_app/views.py 2 from django.views.generic import TemplateView 3 4 5 class AboutView(SomeMixin, TemplateView): 6 template_name = "about.html"

Page 18: [Djangocon2015] Demystifying mixins with Django

My first mixin

Page 19: [Djangocon2015] Demystifying mixins with Django

1 # some_app/views.py 2 3 4 class LoginRequiredMixin(object): 5

Page 20: [Djangocon2015] Demystifying mixins with Django

1 # some_app/views.py 2 3 4 class LoginRequiredMixin(object): 5 6 def dispatch(self, request, *args, **kwargs): 7

Page 21: [Djangocon2015] Demystifying mixins with Django

1 # some_app/views.py 2 from django.core.exceptions import PermissionDenied 3 4 5 class LoginRequiredMixin(object): 6 7 def dispatch(self, request, *args, **kwargs): 8 if not request.user.is_authenticated(): 9 raise PermissionDenied 10

Page 22: [Djangocon2015] Demystifying mixins with Django

1 # some_app/views.py 2 from django.core.exceptions import PermissionDenied 3 4 5 class LoginRequiredMixin(object): 6 7 def dispatch(self, request, *args, **kwargs): 8 if not request.user.is_authenticated(): 9 raise PermissionDenied 10 11 return super(LoginRequiredMixin, self).\ 12 dispatch(request, *args, **kwargs) 13

Page 23: [Djangocon2015] Demystifying mixins with Django

1 # some_app/views.py 2 from django.views.generic import TemplateView 3 4 5 class AboutView(LoginRequiredMixin, TemplateView): 6 template_name = "about.html"

Page 24: [Djangocon2015] Demystifying mixins with Django

LoginRequiredMixin TemplateView

AboutView

Page 25: [Djangocon2015] Demystifying mixins with Django

LoginRequiredMixin DetailView

AboutView

Page 26: [Djangocon2015] Demystifying mixins with Django

ListView

LoginRequiredListView

AboutView

CreateView

LoginRequiredCreateView

AboutView

DetailView

LoginRequiredDetailView

AboutView

FormView

LoginRequiredFormView

AboutView

MyView

LoginRequiredMyView

AboutView

TemplateView

LoginRequiredTemplateView

AboutView

Page 27: [Djangocon2015] Demystifying mixins with Django

LoginRequiredMixin TemplateView

AboutView

Page 28: [Djangocon2015] Demystifying mixins with Django

dispatch()

get_context_data()

get_template_names()

check if user is logged in, has permission

add new data to the context

add more flexibility tothe template names

Page 29: [Djangocon2015] Demystifying mixins with Django

1 # some_app/views.py 2 from django.views.generic import TemplateView 3 4 5 class AboutView(TemplateView): 6 template_name = "about.html"

Page 30: [Djangocon2015] Demystifying mixins with Django

dispatch()

get_context_data()

get_template_names()

check if user is logged in, has permission

add new data to the context

add more flexibility tothe template names

Page 31: [Djangocon2015] Demystifying mixins with Django

docs.djangoproject.com/en/1.8/ref/class-based-views/base/

Page 32: [Djangocon2015] Demystifying mixins with Django

docs.djangoproject.com/en/1.8/ref/class-based-views/base/

Page 33: [Djangocon2015] Demystifying mixins with Django

docs.djangoproject.com/en/1.8/topics/class-based-views/mixins/

Page 34: [Djangocon2015] Demystifying mixins with Django

docs.djangoproject.com/en/1.8/topics/class-based-views/mixins/

Page 35: [Djangocon2015] Demystifying mixins with Django

ccbv.co.uk/

Page 36: [Djangocon2015] Demystifying mixins with Django

django-braces

AccessMixins

FormMixins

OtherMixins

Page 37: [Djangocon2015] Demystifying mixins with Django

With great power comes great

responsibility

Page 38: [Djangocon2015] Demystifying mixins with Django

Recap

• single responsibility• plug-in functionality• isn’t creating a subtyping

relation

Page 39: [Djangocon2015] Demystifying mixins with Django

Go back to your views and start writing mixins to clean up the code.