Python for Entrepreneurs Transcripts
Chapter: Build web apps with Pyramid: Applied web development
Lecture: Getting started with handlers demo

Login or purchase this course to watch this video and the rest of the course contents.
0:02 So let's create our first handler, or like I said, I am going to be calling them from now on controllers.
0:10 And notice I have hacked up the mytemplate page and let's just run the site real quick to see what things look like here.
0:16 OK, I threw away all that starter stuff and now we just have a really simple 3-point navigation
0:21 and a basically something that tells us which page we're on. So this is not handlers yet, this is just me tweaking the template,
0:30 down here, it's run bu this view here. Now because we are building professional, long live, large web applications,
0:37 we want to add a little more structure than just "hey, here is a single file that contains a ton of stuff in it", that's not great as this thing grows,
0:45 so first thing we are going to do is we are going to create a little more structure here
0:48 so we are going to go over here and we are going to create a directory called controllers, and then in the templates, we are going to create
0:57 a grouping of template files for each controller file. So let's go over here, create a file, I'll call this "home_controller",
1:07 because I want to build a home controller class, and for each controller, whatever the prefix name is here
1:12 I am going to put a folder in here now I have one called home, later, we are going to need one for say... albums,
1:25 so we are going to use that part of the name down here as well. OK, so you can see there is going to be an each controller a number maybe 4, 5,10
1:35 who knows it depends on how complicated it is, a number of action methods. Each action method typically has a corresponding template,
1:43 so let's copy that and paste it over here and call this index, what we are going to do for our method is
1:49 we are going to have three methods here, "index", "about" and "contact", so let's just go over here make those, there is CTRL+C, CTRL+V
2:01 great and then let's quickly fix these up so we actually know which page we are on, this is going to be the contact page,
2:08 the contents of it aren't super important, maybe we can pass a single piece of information here, let's just say...
2:15 maybe we'll just do something like this where we pass in some value, who knows.
2:21 Just to show that each action method is actually doing something different. Great, so we have our "index", "about" and "contact".
2:29 Let's go and add the controllers. So recall, the first thing we've got to do is import pyramid handlers, now, PyCharm grays this out saying
2:40 "hey. you are not really using this yet", no not yet, but we will. The name of this class is basically immaterial,
2:48 we just need to use it when we do the mapping but just for me I like to have it the name of the grouping, controller
2:56 and so that's how I am going to call this, and then we are going to add three methods, we are going to have a "def index", for now I'll just say "pass"
3:02 and then we'll do this for about and we'll do it for "contact". Do a little format, great. OK, so remember what we need to return here is a dictionary.
3:11 And we also need to tell the "index", "about" and "contact" methods what template they should use to render that dictionary,
3:20 so we'll say @pyramid_handlers.action and we'll say "render=" it's going to be "templates/home/", this one is going to be index.pt.
3:34 And "about", "contact". Maybe some day I'll come up with some alternate implementation here
3:44 that when you call these functions, if it doesn't have a render, it looks for, you know, something like in this pattern, but right now,
3:49 you have to explicitly say this. So let's try go into say like /home/about see what happens. Spoiler alert- it's not going to work.
3:59 So if I go here, you can see 404 not found. Remember, creating the controller is not enough, we also have to map it.


Talk Python's Mastodon Michael Kennedy's Mastodon