Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django TemplateResponseMixin

I am new to Django and trying to get my head around mixins with class based views. As per my understanding, mixins are classes containing a combination of methods from other classes(similar to multiple inheritance).

I am following the official documentation and searched for questions on SO, but did not understand why TemplateResponseMixin is a mixin per se. What methods of different classes are being injected into the TemplateResponseMixin?

like image 889
Blackhole Avatar asked Dec 08 '22 04:12

Blackhole


2 Answers

You can think of mixins as partial classes which are used to enrich another class by providing extra functionality. However, mixins itself do not contain enough information in order to constitute a complete class but they offer new features to classes they are added into.

In your case, TemplateResponseMixin alone can't be considered as a View class because it has lack of many crucial methods like get, post or dispatch that will have us identify it as an actual view.

However, together with the help of BaseView (or View) class, it creates another form of view named TemplateView and gives that the ability of rendering a template via render_to_response method unlike normal view.

A good metaphor would be the human body. It is composed of different layers, all of them are specialized in doing something but, the bottom line is that, they all need some kind of skeletal system first in order to be kept together and stay meaningful. Thus, in this scenario, human skeleton (View) would be considered as the "thing" that makes entire system meaningful and we would wrap that skeleton up with a skin (TemplateResponseMixin) in order to make it look like more human.

Re-interpreting this concept using Django's class-based view approach, we get;

class View(BaseView):
    def dispatch(self, request, *args, **kwargs):
        if request.method == GET:
            self.get(request, *args, **kwargs)
        elif request.method == POST:
            self.post(request, *args, **kwargs)
        ...

As you see, view has no idea whether it defines get() method or not, however it knows that someone will define that in some other layer (TemplateView.get) that will be responsible for handling the incoming request.


from django.shortcuts import render 

class TemplateResponseMixin(object):
    template_name = None

    def render_to_response(self, request, context, **response_kwargs):
        return render(request, self.template_name, context)

TemplateResponseMixin alone doesn't give us any clue in regard to its real purpose and render_to_response itself makes no sense at all. However, together with the view, it will be used for rendering a template page using view's logic (in other words skeleton).

class TemplateView(TemplateResponseMixin, View):
    template_name = 'some_app/some_template.html'

    def get(self, request, *args, **kwargs):
        context = get_context_from_somewhere(request, *args, **kwargs)
        return self.render_to_response(request, context)

There is a very educational SO thread here about mixins. I'd recommend you to read all answers very carefully.

like image 91
Ozgur Vatansever Avatar answered Dec 11 '22 07:12

Ozgur Vatansever


TemplateResponseMixin - A mixin that can be used to render a template.

It has two methods -

render_to_response() and get_template_names()

For more info refer here

like image 25
ancho Avatar answered Dec 11 '22 09:12

ancho