Slide 1

Slide 1 text

Demystifying Mixins with Django

Slide 2

Slide 2 text

@anabalica

Slide 3

Slide 3 text

Mixins are a controlled way of adding functionality to classes.

Slide 4

Slide 4 text

Mixins are not special language constructs.

Slide 5

Slide 5 text

In fact, mixins are ordinary Python classes. 1 class SomeMixin(object): 2 """My smart mixin""" 3 4 def test_method(self): 5 pass

Slide 6

Slide 6 text

Why use mixins? to improve modularity

Slide 7

Slide 7 text

When to use mixins? want to reuse a particular feature in a lot of different classes

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

In Python the concept of mixins is implemented using multiple inheritance.

Slide 12

Slide 12 text

Order matters

Slide 13

Slide 13 text

1 class Foo(BaseFoo, SomeMixin): 2 pass base class mixin

Slide 14

Slide 14 text

1 class Foo(BaseFoo, SomeMixin): 2 pass

Slide 15

Slide 15 text

1 class Foo(SomeMixin, BaseFoo): 2 pass

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

My first mixin

Slide 19

Slide 19 text

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

Slide 20

Slide 20 text

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

Slide 21

Slide 21 text

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

Slide 22

Slide 22 text

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

Slide 23

Slide 23 text

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

Slide 24

Slide 24 text

LoginRequiredMixin TemplateView AboutView

Slide 25

Slide 25 text

LoginRequiredMixin DetailView AboutView

Slide 26

Slide 26 text

ListView LoginRequiredListView AboutView CreateView LoginRequiredCreateView AboutView DetailView LoginRequiredDetailView AboutView FormView LoginRequiredFormView AboutView MyView LoginRequiredMyView AboutView TemplateView LoginRequiredTemplateView AboutView

Slide 27

Slide 27 text

LoginRequiredMixin TemplateView AboutView

Slide 28

Slide 28 text

dispatch() get_context_data() get_template_names() check if user is logged in, has permission add new data to the context add more flexibility to the template names

Slide 29

Slide 29 text

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

Slide 30

Slide 30 text

dispatch() get_context_data() get_template_names() check if user is logged in, has permission add new data to the context add more flexibility to the template names

Slide 31

Slide 31 text

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

Slide 32

Slide 32 text

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

Slide 33

Slide 33 text

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

Slide 34

Slide 34 text

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

Slide 35

Slide 35 text

ccbv.co.uk/

Slide 36

Slide 36 text

django-braces Access Mixins Form Mixins Other Mixins

Slide 37

Slide 37 text

With great power comes great responsibility

Slide 38

Slide 38 text

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

Slide 39

Slide 39 text

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