Python for Entrepreneurs Transcripts
Chapter: Build web apps with Pyramid: Applied web development
Lecture: Mapping routes to controller actions demo
Login or
purchase this course
to watch this video and the rest of the course contents.
0:01
All that stuff happens down here in the entry point, in the dunder init. Now, this is a pretty simple implementation right here,
0:10
but as it grows, we kind of want to group these various pieces and so let's do a little work to reorganize this to make things a little nicer,
0:18
so if I highlight those two things, I hit CTRL+T, I can say "make that into a method" and I'll say this will be init_includes or something like that
0:27
so down here you can see we are now just calling this function and we can do this for the init_routing,
0:36
OK so like I said, as this grows, we kind of want to have a bunch of pieces, init_login, init_database etc.
0:43
OK, when you see this config.add_route, this is for the traditional routing, the ones that go straight on functions with the @view_config, like so.
0:53
And we are not going to use this one, although I'll leave it in just for a moment, we are going to say config.add_handler,
1:02
and in here we are going to specify a route name just like you have "home" above, I'll call this home_ctrl, something like this,
1:10
and then I need to give it a URL, so let's say the URL is going to be /home/ and then we get our flexibility by saying {action},
1:18
so if we have the action method here, like so, then we can just have this one route and it maps to /home/index, /home/about,
1:26
/home... whatever is on that class basically. And then at the end we have to set the handler to be equal to a thing,
1:31
so now we've got to come up here and start importing our handlers, so we'll say import, now remember this is a package
1:38
so you have to say the full name "blue_yellow_app.controllers.home_controller as home",
1:45
just to save some sanity down here let's say home.HomeController. OK, so this should be enough to get started,
1:52
let's just run it and verify that that's the case. Again, this is not running that particular- I think this is going to crash
2:00
the way I wrote it, I'm saying there is a missing piece of data from the model, oh no actually there is one thing I forgot, I'm very sorry about this,
2:08
let's go back to the controller, all the controllers, if I have the basic controller implemented
2:13
I wouldn't need this, but for now, that's why I forgot it, but for now, we are going to need the dunder init methodm so w'll say "def __init__"
2:21
and it has to take a request object, so notice, over here, we are passing in the request to be used, here we don't pass the request to the method
2:31
we actually pass the request in the initializer and then it's available for all the methods, so let's use PyCharm to store that like so, now we run it,
2:40
now we'll see my dictionary error I was hoping for. Renderer was passed in non-dictionary value. Alright, so what do we need to pass along here?
2:49
So we need to return a dictionary and we said there was going to be a value
2:53
and the value is going to be HOME, I'll just put all-caps HOME just so we can see.
2:58
The other thing we need to pass is we need to pass that CSS cache busting thing,
3:02
that we worked on before, I'll show you how we don't ever have to do this again but just for now so it works, we are going to pass it.
3:11
Alright, let's try it one more time. Come over here /home, beautiful, here is the "home" controller, the value from the controller is "home" index.
3:20
Now, again, if we go to "about", it is going to have the same problem, renderer was passed non-dictionary value, but notice home/index
3:27
and home/about are maps, let's just go fix up these other two. Now, in development mode when you make some changes
3:42
and you go over here and refresh, on the templates, that works, but for the Python code you have to restart the app, we are going to restart, reload,
3:51
so here we have home, about, contact. Look how cool that is. Ok, so it looks like everything is working now, if I go here, notice,
3:59
this is not the same home view, so let's go and work with the routing just a bit to fix this. so over here I would like to have really just home/index
4:09
also stand in for "/", so let's comment that out, I want to do another one, I will just say "call this root" or something like that
4:17
we'll just say the URL is going to be /home, now we come over here and say the action is going to be index, with an n.
4:24
OK, so this tells the Pyramid handler system "look, if somebody comes to "/", just render the home/index",
4:32
otherwise, render using this sort of hierarchical action-based routing. Now the other thing we are going to have to do is
4:41
we are going to have to go over to our view and not try to map it to a thing called "home",
4:45
which doesn't exist, so let's just entirely comment that out, in fact let's just go for broke, delete it.
4:52
I got a warning because it's used in the tests down here but that's OK. Put those on hold for a minute. OK, let's try again.
5:02
Now, check this out, we're up here on "/" and we get value of "controller is HOME", about, over here /index or like this, same thing.
5:12
Now of course we probably want to change this link up here to just be "/" but you can see how we can tweak the routing to make this really nice,
5:19
let me show you one more hang up you are going to run into on a routing,
5:21
so if somebody says /home/index/, well that doesn't match to the regular expression
5:26
so too bad, so it's easy to fix, just come down here and we'll just add another one,
5:31
now this name is a dictionary key, so it has to be unique, so we'll say a little "/" like this, like so and that will let us map this little "/"
5:40
and then some of these also take data, right, the home/about, the home/index and so on don't. But if you have like albums, details,
5:49
you want to pass in like a number and so it's very common to have...
5:52
like I had in my slides, some kind of of id here, so we'll say something like id, like this.
5:58
Now this part looks almost the same for every single handler or controller you are going to setup.
6:06
So at some point what we'll do is we'll write a method where we pass in that piece of data, and that bit of data
6:15
and we'll just let it build these groups of routing for us. Finally let's do a little more cleanup, let's delete this.
6:24
Great, our website is now using handlers for all of its work.
6:31
This is not required, but I find this to be a much better way to organize large applications,
6:36
it's hard maybe to convince some of you if you just want to write functions, but as you go through it, you'll see here is one more cool feature,
6:43
here is one more cool feature, and they will just build up until you are like "OK, I get it". And if not, it's fine, just use the other style.