What is a Middleware?

Photo by Jennifer Latuperisa-Andresen on Unsplash
  • The SessionMiddleware class is initialized with attributes of get_response HttpResponse object, engine module, SessionStore class got from the engine . By default, in Django SESSION_ENGINE is database backed.
  • process_request checks if there’s a cookie named sessionid in request.COOKIES dictionary. sessionid is a default value for settings.SESSION_COOKIE_NAME . request object’s session attribute is assigned SessionStore object , that was initialized in the class, with or without session_key .
  • process_response checks if we need to create, update or delete cookie with all possible scenarios based on whether SessionStore is empty; sessionid has already been accessed; or SessionStore has been modified.

--

--

Software Engineer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store