Middleware classes are not fancy; they simply have one or more methods that correspond to one of the five predefined hooks available. Let's take a closer look at them here.
- View Offline
- I think the two most used middleware hooks are process_request and process_exception. So I'll demonstrate each of these. In this video we'll cover process_request. Middleware is a class that defines one or more methods that correspond to one of the hooks mentioned in the last video. We'll create a simple class called UserProfileMiddleware and we'll define a method called process_request that accepts the request. Because we're using the process_request hook this method will be called on each and every request unless an earlier request stops it.
In the case of process_request, we can stop the view handler and all of the rest of the request middleware from being called if we return an Http response object. For example a redirect. This middleware won't need to do that, so we need to insure that we simply return None. We have to do one or the other though, either None or an HTTP response. As you might have guessed from the name this middleware will try to get the users profile out of the cache. If the user's logged in and the profile exists we'll attach it to the request.
Notice here that the first thing we do is check to see if the user is authenticated. If not we simply return None. This middleware is now done. If the user is logged in then we'll keep going and try to get their profile out of the cache. If that doesn't work then we'll fetch their profile and then cache it. Note that it's possible that the profile does not exist so we'll handle that here and create an empty profile. This is an empty function, You'd replace this with the actual code that you would use to create the new profile which can vary depending on which version of Django you're using.
Now that we have a valid profile we attach it to the request and then return None. The next middleware review will have access to the user profile as part of the request object. Now to enable it we need to visit settings.py. We'll look at the middleware classes configuration. Since we're using the user.IsAuthenticated method we need to do this after the session and authentication middleware. There is no compelling reason to put this before these so we'll just add it to the end. Do note that in some cases the order does matter.
For example you may need to do this before the authentication. For example you may want your middleware to run before authentication. I also want to point out that we only defined one method. The one for process_request. It may be that the middleware actually handles multiple hooks. In which case it's okay to have multiple methods. And likewise it could be that I want to extract some of my code into smaller methods. In which case I'm fine as long as I don't accidentally name my method after one of the hooks. Also note that it doesn't matter if we're using the new style or a simple Python classes either one will work fine.
In the next video we'll make middleware for handling exceptions.
Django is one of the world's leading web development platforms. This Python web framework makes your app building experience smoother, faster, and easier. It is a key feature behind the success of many start-ups across the world, including Instagram and Nextdoor. This course introduces next-level advanced concepts to help you build your own professional-grade Django applications.
Author Kevin Veroneau starts off by explaining how class-based views—a powerful, but often poorly understood feature—work in Django. Then learn how to create custom middleware to help organize frequently used code. Have you ever wanted to build a complete RESTful API? Find out how to do just that, starting with a basic read-only API and then adding key-based authentication to allow the full range of CRUD functionality. Plus, discover how to use model managers and the Q method to make it easier to get the data we need to our views and clients.
For the finishing touches, dive into the details of caching to improve application performance, and then add custom management commands to help automate routine tasks.
- Extending class-based views
- Creating custom class-based views
- Managing your database with middleware
- Creating template filters and tags
- Building a RESTful API
- Modeling advanced database relationships and queries
- Using a model manager for your application