Showing users exceptions is unprofessional and may be a security problem. Let's examine an easier way to create middleware that catches and responds to unhandled exceptions.
- [Instructor] Let's talk about something you should…always use in your apps.…Middleware to handle exceptions.…Normally, all of your exceptions are handled, right?…All right, sometimes an exception may sneak through.…Well, as you'll see here in just a second,…this is so easy, and the benefits are so good,…it's foolish not to use this middleware hook.…Like in the last video, our middleware is pretty simple.…We simply need to create a class that has a method…for the proper hook.…In this case, the process exception hook.…This method accepts two parameters,…the request and the exception.…In this example, we'll simply check to see…if the exception is an app error, and if so,…we'll redirect to the homepage.…
Otherwise, we return none.…We need to add this to the list of middleware…in settings.py.…Note that for the process exception hook,…the order is bottom to top.…There isn't really a big concern about the order…in which this one is called,…so we'll just keep it at the bottom.…Here's how we might use this in a view.…We check to see if the user is staff.…
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