How a request is processed in Django?

devquora
devquora

Posted On: Feb 22, 2018

 

In Django whenever a request is made by a user, it goes through the following steps:

  • Django determines the root URLconf module to use. Ordinarily, this is the value of the ROOT_URLCONF setting, but if the incoming HttpRequest object has a urlconf attribute (set by middleware), its value will be used in place of the ROOT_URLCONF setting.
  • Django loads that Python module and looks for the variable urlpatterns. This should be a Python list of django.urls.path() and/or django.urls.re_path() instances.
  • Django runs through each URL pattern, in order, and stops at the first one that matches the requested URL.
  • Once one of the URL patterns matches, Django imports and calls the given view, which is a simple Python function (or a class-based view). The view gets passed the following arguments:
    • An instance of HttpRequest.
    • If the matched URL pattern returned no named groups, then the matches from the regular expression are provided as positional arguments.
    • The keyword arguments are made up of any named parts matched by the path expression, overridden by any arguments specified in the optional kwargs argument to django.urls.path() or django.urls.re_path().
    • If no URL pattern matches, or if an exception is raised during any point in this process, Django invokes an appropriate error-handling view.

    Related Questions

    Please Login or Register to leave a response.

    Related Questions

    Django Interview Questions

    What does Of Django Field Class types do?

    What does Of Django Field Class types do?..

    Django Interview Questions

    Does Django Follow Architectural pattern?

    Does Django Follow Architectural pattern?..