what is dispatch used for in django?

前端 未结 2 497
醉梦人生
醉梦人生 2021-02-04 02:50

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.

2条回答
  •  忘掉有多难
    2021-02-04 02:58

    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.

提交回复
热议问题