Building Data-Driven Web Apps with Pyramid and SQLAlchemy Transcripts
Chapter: Routing and URLs
Lecture: Adding the package-details route
0:00 The last thing to do, let's extend this
0:02 let's have, I'm just going to copy and paste
0:05 it'll be a little quicker
0:06 I'm going to call this AccountController.
0:10 And PyCharm asks, can we add this to Git
0:13 I'm going to just say yes you can.
0:15 So we go over to our AccountController
0:17 I'm going to get rid of most of this.
0:19 The name has to be changed, so let's just
0:21 comment this out for a second.
0:23 But just so we have the pieces in place
0:25 and we'll do the same thing for packages.
0:29 Packages, we're going to write some bits here.
0:32 OK, so for packages, remember
0:34 what were looking for, is we're looking for /project
0:39 that's the way it works.
0:40 Let's just remind ourselves.
0:42 PyPI.org, if we go over to any particular project
0:46 and we click on this, notice the url
0:48 is /project/the name of the package
0:52 with a /.
0:54 Okay, so that's what we're aiming for
0:57 something like that.
0:59 And let's put a little project, package
1:05 name bit in there.
1:07 So this is the route that we're looking for.
1:09 And let's call that package_details.
1:18 And this is going to be packages
1:21 uh, not index, how about details?
1:24 For now, let's just return no data.
1:28 We'll return the package name in just a minute.
1:32 Okay, great, this is looking a little broken
1:35 but it's actually not broken
1:36 that's just PyCharm not quite refreshing itself correctly.
1:39 So we've got our home, it says home
1:42 let's just call this details.
1:45 It's going to be contained in the packages
1:47 so it'll be packages.details, that's
1:48 we don't really need, since we've broken this
1:51 up into the modules by category
1:53 we don't need to put them on the method names.
1:56 So we need to add this.
1:57 If we try to run this now
2:01 and we refresh, well, actually, it doesn't even run.
2:04 Didn't make it that far.
2:05 If we come over here and we look
2:07 you'll see, no route named package_details
2:11 found in view registration.
2:13 Alright, so the next thing to do is going to be go fix that.
2:16 So that brings us back to this method
2:18 we were working on before.
2:20 So we've got our stuff to do with home_controller
2:24 maybe, we'll put it like that, I don't know.
2:26 Over here we'll have packages
2:28 or PackageController.
2:31 And we're just going to do the same thing.
2:34 and we're going to give it a name.
2:36 What'll we call it, project_details, I think?
2:39 Let's look, no, package_details.
2:42 So we'll say package_details
2:45 and the url is actually going to be /project
2:49 / whatever the name is.
2:51 Now, something that's a little bit annoying
2:54 with Pyramid routes is
2:56 whether that's there or not makes a big difference.
2:59 And if you want to let them put it there
3:00 or not put it there, it's not a great option.
3:03 So what we can do is, like
3:05 have two routes, one with a /
3:07 and one without a /, it's okay.
3:10 It's not great, but it'll do.
3:12 So if we come over here, for that to work
3:17 we're going to have to have both routes here.
3:20 So let's rerun this, and it should just say
3:23 the details is not found, so we'll fix that next.
3:25 But let's see that we can get a little farther
3:27 what we did, it ran, that's good.
3:29 If we refresh it, we come over here to
3:37 Great, details wasn't found.
3:38 So the next thing we have to do is just put the details
3:40 in there, easy enough.
3:44 So let's go down here and
3:46 what's a great example?
3:47 Let's take about is pretty simple.
3:49 Let's paste that as details
3:52 just copied and pasted in PyCharm.
3:54 We'll go over here and we'll have package
3:57 and I'm just going to put the name, package name.
4:09 We'll figure out the details when we get
4:10 to the data access section
4:12 but let's just work on the route to make sure
4:14 this is loading the correct one, okay?