The HttpRequest object contains useful information about the current user, language, server variables, cookies, session, and so on. As a matter of fact, HttpRequest is provided in the views and middleware, and you can pass it (or its attribute values) to forms, model methods, model managers, templates, and so on. To make life easier, you can use a so-called ThreadLocalMiddleware that stores the current HttpRequest object in the globally accessible Python thread. Therefore, you can access it from model methods, forms, signal handlers, and any other places that didn't have direct access to the HttpRequest object previously. In this recipe, we will define such a middleware.
Using ThreadLocalMiddleware
Getting ready
Create...