Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is dispatch used for in django?

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):         try:             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)         except:             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)         else:             raise Http404 
like image 711
Taylor Hardie Avatar asked Dec 14 '17 08:12

Taylor Hardie


People also ask

What are Middlewares in Django?

Middleware is a framework of hooks into Django's request/response processing. It's a light, low-level “plugin” system for globally altering Django's input or output. Each middleware component is responsible for doing some specific function.

What are Mixins in Django?

In object-oriented programming languages, a mixin is a class which contains a combination of methods from other classes. Add timestamp and trashed attribute for every model object - This was one of the most important thing that we should have been implemented as early as possible.

What is context in Django?

A context is a variable name -> variable value mapping that is passed to a template. Context processors let you specify a number of variables that get set in each context automatically – without you having to specify the variables in each render() call.


Video Answer


2 Answers

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.

like image 99
sytech Avatar answered Sep 28 '22 03:09

sytech


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.

like image 21
Richard Ackon Avatar answered Sep 28 '22 02:09

Richard Ackon