Django concepts

In general, explaining Django internals is out of scope for this documentation. However, for some concepts it is useful to at least briefly provide some information. Whenever Django (ie Django’s WSGI entrypoint) receives a request. The following happens:

  • The request is routed using URL Patterns to a view (function)

  • Before being given to the view, the request is passed through various Middleware

  • The view processes the request and builds a reponse, often using Templates

  • The response is passed through the Middleware in reverse order before being sent out

URL Patterns

When the request is first accepted, it is routed to a View or view function. This is done through urlpatterns defined in various urls.py files. The root URL Patterns file is defined in the ROOT_URLCONF setting. This file contains a urlpatterns attribute that has a list of paths. Paths define a url prefix and a target. A target may be a view function, or may further dispatch a request to another urls module, in which case the path’s url prefix is stripped from the url. In case the target is a view function, that function will be called to generate a response. But first the configured Middleware are called.

See also: Django Documentation: URL dispatcher

Middleware

Middleware are functions or classes that receive a http request, can pre-process that request and pass it through to the next Middleware, and eventually the View. Common use cases for middleware are:

  • Authentication

  • Session managment

  • Other security aspects, such as CSRF protection

Which Middleware is active is configured using the MIDDLEWARE setting. See also Django Documentation: Middleware

Templates

When responding to a HTTP request in Django, you often send the result of a rendered template as a response. In the view function

See also: Argus Documentation: Override Templates

Management commands

django admin vs manage.py, DJANGO_SETTINGS_MODULE. various builtin management commands