What is dispatch used for in django?

Each Answer to this Q is separated by one/two green lines.

I have been trying to wrap my head around the dispatch method, particularly in Django. However, I cannot seem to figure out exactly what it does. I tried to gain an understanding from the Django docs but didn’t find them to informative on this topic. Per my understanding it is a listener that listens to all events happening on a page but I am not sure if this is the case?

class OrderDetail(DetailView):
    model = Order

    def **dispatch**(self, request, *args, **kwargs):
            user_check_id = self.request.session.get("user_checkout_id")
            user_checkout = UserCheckout.objects.get(id=user_check_id)
        except UserCheckout.DoesNotExist:
            user_checkout = UserCheckout.objects.get(user=request.user)
            user_checkout = None

        obj = self.get_object()
        if obj.user == user_checkout and user_checkout is not None:
            return super(OrderDetail, self).dispatch(request, *args, **kwargs)
            raise Http404

The dispatch method takes in the request and ultimately returns the response. Normally, it returns a response by calling (ie, dispatching to) another method like get. Think of it as a middleman between requests and responses.

Normally, it simply decides what method in the class (e.g. get(),post(), etc) should be used (ie, dispatched) based on the HTTP method that was used in the request. Something like

def dispatch(self, request, *args, **kwargs):
    if request.method == 'GET':
        return self.get(*args, **kwargs)
    elif request.method == 'POST':
        return self.post(*args, **kwargs)
    elif #... and so on

You can use your own dispatch method to change this behavior to call whatever methods you want that should return the HTTP response or even ‘intercept’ and modify the arguments that ultimately reach those methods. For example, you might use this to block/filter certain kinds of requests or even inject arguments…

def dispatch(self, request, *args, **kwargs):
    """Updates the keyword args to always have 'foo' with the value 'bar'"""
    if 'foo' in kwargs:
        # Block requests that attempt to provide their own foo value
        return HttpResponse(status_code=400)
    kwargs.update({'foo': 'bar'}) # inject the foo value
    # now process dispatch as it otherwise normally would
    return super().dispatch(request, *args, **kwargs)

But the key concept is that it’s the entry point for requests and ultimately responsible for returning the response.

When a request url matches a url in your urls.py file, django passes that request to the view you specified. The request can only be passed to callable functions. This is why when using class-based views, you use the as_view() method. The as_view() method returns a function that can be called.

This function then creates an instance of the view class and calls it’s dispatch() method. The dispatch method then looks at the request and decides whether the GET or POST method of the view class should handle the request.

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .