Python for Entrepreneurs Transcripts
Chapter: Build web apps with Pyramid: Applied web development
Lecture: Introducing Pyramid handlers: Classes as controllers
0:03 In the next few lectures, we are going to look at this concept, this package called pyramid_handlers. Pyramid handlers are a different, alternative way
0:12 to basically group and map routes to action methods, or methods that process the request. Then while Pyramid handlers are not required
0:22 for building very advanced websites with Pyramid, you'll see that they do have a few key advantages,
0:29 for example, they let us group common functionality, these handlers, and I am going to go and start calling them controllers,
0:36 after we get started with them. But just while we are introducing it, the package designer Chris McDonough, same guy who created Pyramid,
0:43 calls them handlers, so... these handlers let us use classes to group common functionality, both in terms of related web methods,
0:54 so related action methods that are processing different requests, so for example if you have like a user account management thing, we might have
1:02 a handler called "account" and then there might be a register / log in / log out. And those might be all grouped into the same class
1:09 and you can imagine there will be other functionality private methods and whatnot that will be used across those different action methods
1:16 as you work with users. We also see there is less routing work. Now, there is two ways to reduce the routing work,
1:23 what you've seen so far with starter scaffold is you have to be extraordinarily specific and every time you want a separate method to be called,
1:30 a separate view method, you have to make a route, name that route and apply it as a decorator to that method,
1:38 and that gets really tedious in real applications and it gets tedious quickly. so there is two ways to go around that.
1:44 One - you can use this concept called traversal, which is one of the other alternative scaffolds,
1:50 or we can use this concept of mapping the routes to classes and then only when we are adding a new group of functionality,
1:57 do we actually have to go and fiddle with the routing. And finally, the core web infrastructure is always going to be accessible.
2:04 We can create a base handler class, and all of our handlers or controllers can derive from that,
2:11 that means a bunch of functionality that has to be universally accessible, will be.
2:16 So, we'll see that if we have like a common check for a cookie, like authentication, or particular logged in user, or some other common functionality,
2:27 we can put that into the base controller and then any time we add some new group of functionality, it will automatically pick that up.
2:35 We'll also see that using handlers allows us to create shared templates and layouts that let us have a common look and feel in a super easy way.