在django中派遣有什么用? [英] what is dispatch used for in django?

查看:93
本文介绍了在django中派遣有什么用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直试图把头放在分发方法上,尤其是在Django中(请参见下面的代码示例)。但是,我似乎无法确切地了解它的作用。我试图从Django文档中获得理解,但没有找到有关此主题的有益信息。据我了解,这是一个侦听器,它侦听页面上发生的所有事件,但是我不确定是否是这种情况?

I have been trying to wrap my head around the dispatch method, particularly in Django (please see code example below). 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? Thanks.

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


推荐答案

dispatch方法接受请求并最终返回响应。通常,它通过调用(如 get 之类的另一种方法(即 dispatching )来返回响应。

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.

通常,它只是决定类中的哪种方法(例如 get() post()等)应基于请求中使用的HTTP方法使用(即已分派)。

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

您可以使用自己的调度方法更改此行为以调用您想要返回HTTP响应甚至拦截的任何方法,并修改最终到达这些方法的参数。例如,您可以使用它来阻止/过滤某些类型的请求,甚至可以插入参数...

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.

这篇关于在django中派遣有什么用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆