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.
0:03 If I come over and refresh it, it's going to say
0:06 well now you say there's a package name
0:08 and there's not.
0:09 Let's fix that last thing.
0:11 So the question is now
0:12 how do I get that package name?
0:15 Remember, it's passed into request
0:17 which we now need to use this, so put its name back.
0:21 And then we want to get the name out.
0:23 So here's a quick simple one.
0:26 Package, name, it's going to be request.matchdict
0:30 notice no autocomplete, watch this.
0:33 Go over here and say request.
0:37 So, if we import this, if we say from pyramid
0:43 request, import, request
0:46 and now come down here and say dot
0:48 we get all sorts of stuff, including
0:51 matchdict, which is a dictionary.
0:54 And in this, this where it's been passed
0:56 in the URL, has that variable.
0:58 So we do a get on it.
1:01 Then we'll just come down here and say
1:03 package name, whoops, this needs to be in quotes.
1:08 Let's re-run it, and see how we're doing.
1:12 There's requests, what if we
1:14 go for sqlalchemy?
1:17 There, sqlalchemy, and so on.
1:19 Notice how the routing is passing it along.
1:23 Here are the details for pyramid
1:25 of course we don't actually have that data
1:27 but we can get the structure in here
1:29 and then we'll just create hyperlinks
1:30 out of the database, or out of our fake data
1:32 either one, that point back to something like this.
1:35 Okay, so this route is working really well.
1:38 We've a few more we want to add
1:40 let me do a little clean up here.
1:42 Also, you'll notice that this
1:46 we're not testing this here so it could be
1:50 this comes back as None because for some reason
1:53 it didn't get passed or, more realistically we
1:55 went to the database and they did a query for
1:57 a thing that didn't exist in our database.
2:00 So we can say, if not package name
2:02 then we can do, raise
2:06 say import pyramid
2:10 httpexceptions as x.
2:15 I'm going to go x.HTTPNotFound.
2:20 That'll I'll just tell whoever's listening
2:23 it wasn't found.
2:26 Let's suppose, just test this really quick.
2:30 If we go ask for gone, let's see what we get.
2:34 So here we can ask for pyramid, everything's great
2:36 but if I go and say gone, boom, not found.
2:40 So that little bit isn't working.
2:43 Of course this doesn't make sense.
2:45 But if we don't get past package name or we try
2:47 to do a query and it doesn't exist
2:49 then we're going to tell them it's not here
2:50 otherwise, it will return the details of it like this.
2:53 We're going to get much more advanced when we get
2:54 to the database section, but this interaction
2:57 with the routing is pretty typical.
2:59 So, a request comes in, it has matchdict the
3:03 values passed in the route like so
3:05 appear in here and we interact with it however we want.
3:10 We get the value back, maybe we talk to the database
3:12 call a web service, whatever.