Building Data-Driven Web Apps with Pyramid and SQLAlchemy Transcripts
Chapter: Routing and URLs
Lecture: Getting data from route

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Now, this is not going to work. If I come over and refresh it, it's going to say well now you say there's a package name and there's not.
0:10 Let's fix that last thing. So the question is now how do I get that package name? Remember, it's passed into request
0:18 which we now need to use this, so put its name back. And then we want to get the name out. So here's a quick simple one.
0:27 Package, name, it's going to be request.matchdict notice no autocomplete, watch this. Go over here and say request.
0:38 So, if we import this, if we say from pyramid request, import, request and now come down here and say dot we get all sorts of stuff, including
0:52 matchdict, which is a dictionary. And in this, this where it's been passed in the URL, has that variable. So we do a get on it.
1:02 Then we'll just come down here and say package name, whoops, this needs to be in quotes. Let's re-run it, and see how we're doing.
1:13 There's requests, what if we go for sqlalchemy? There, sqlalchemy, and so on. Notice how the routing is passing it along.
1:24 Here are the details for pyramid of course we don't actually have that data but we can get the structure in here and then we'll just create hyperlinks
1:31 out of the database, or out of our fake data either one, that point back to something like this. Okay, so this route is working really well.
1:39 We've a few more we want to add let me do a little clean up here. Also, you'll notice that this we're not testing this here so it could be
1:51 this comes back as None because for some reason it didn't get passed or, more realistically we went to the database and they did a query for
1:58 a thing that didn't exist in our database. So we can say, if not package name then we can do, raise say import pyramid httpexceptions as x.
2:16 I'm going to go x.HTTPNotFound. That'll I'll just tell whoever's listening it wasn't found. Let's suppose, just test this really quick.
2:31 If we go ask for gone, let's see what we get. So here we can ask for pyramid, everything's great but if I go and say gone, boom, not found.
2:41 So that little bit isn't working. Of course this doesn't make sense. But if we don't get past package name or we try to do a query and it doesn't exist
2:50 then we're going to tell them it's not here otherwise, it will return the details of it like this. We're going to get much more advanced when we get
2:55 to the database section, but this interaction with the routing is pretty typical. So, a request comes in, it has matchdict the
3:04 values passed in the route like so appear in here and we interact with it however we want. We get the value back, maybe we talk to the database
3:13 call a web service, whatever.


Talk Python's Mastodon Michael Kennedy's Mastodon