[Django]-What is dispatch used for in django?

65👍

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.

👤sytech

15👍

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.

Leave a comment