Using ThreadLocalMiddleware

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 middlewares, and then 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 the ThreadLocalMiddleware middleware that stores the current HttpRequest object in the globally-accessed Python thread. Therefore, you can access it from model methods, forms, signal handlers, and any other place that didn't have direct access to the HttpRequest object previously. In this recipe, we will define this middleware.

Getting ready

Create the utils app and put it ...

Get Django: Web Development with Python now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.