Python for Entrepreneurs Transcripts
Chapter: Build web apps with Pyramid: Applied web development
Lecture: Concept: Controller classes via Handlers

Login or purchase this course to watch this video and the rest of the course contents.
0:02 Let's see how we define a set of use, or action methods grouped by a common class, by using handlers.
0:09 So, we are going to have to start by importing Pyramid handlers, so of course you want to make sure that we've installed it
0:16 and we just saw how to do that. Once we have it imported, we can use it in any of our custom classes, so we'll begin by creating some kind of class,
0:25 here we have to start calling these controllers because in the NVC world that's absolutely what they are,
0:29 if you look at the documentation for Pyramid handlers it actually says a Pyramid implementation of plone controllers. These are controllers, folks.
0:38 So what we do is we create just a plain class, we'll see later how to enhance this with a base controller concept
0:43 but that's not required for what we are doing. Then, we are going to add a couple of methods to this controller class,
0:49 and these are just standard methods, they take no parameters other than themself, and they return some kind of dictionary or a Pyramid response,
0:58 but often you are going to return a dictionary as a model. Then you do just a standard processing on the index,
1:03 maybe we want to index, in this example is going to be like the home screen. So, maybe what we want to do is show like a list of albums
1:12 or upcoming events or who knows, something like that, right, so somewhere in the middle in that common area
1:18 we are going to access the database, maybe call web service, do some computation and return that data to a template.
1:24 Now, once we've mapped these URLs, we could start calling index in about
1:28 but that's not enough, we need to give it additional information. Most importantly, we need to specify what template we are going to use,
1:35 what template file, so that the system knows what it should display for the HTML.
1:41 So, we bind that and more information together using the action attribute, so here I am using the full namespace, "pyramid_handlers.action"
1:49 and you can set the render to be some kind of template here I've set it to be the index Chameleon template in the home folder,
1:57 we are going to do a little reorganization as we get into this. Similarly down here on the about method, we'll set the "about" template as the render,
2:05 you can put other things in there, for example we could say that we want the JSON serializer and just return JSON objects,
2:11 or all sorts of things, there is quite a few options, and we'll look at them as we go through this section.
2:16 Now, this defines the controller that could handle the request but nothing is going to happen if we write this, we need to actually tell Pyramid:
2:25 "Hey, these URLs map over to these action methods." The way we do that is we map the class as a handler, so we can say config.add_handler,
2:36 again, give the route a name but this time it could be much more general, so we could say /home/{action}/{id} and "action" and "id" are data elements,
2:45 variable pieces that could be passed across, so "action" actually names the method, so home/index will show the first method,
2:53 home/about will show the second. Now the way it's written here, this id is required but you'll see as we get into it, there are a few variations.
3:01 So you could do things like home, home/index and home/index/7, whatever that means.

Talk Python's Mastodon Michael Kennedy's Mastodon