Python for Entrepreneurs Transcripts
Chapter: Build web apps with Pyramid: Applied web development
Lecture: Concept:Template introduction
0:02 It's time to write some code that actually generates some interesting HTML,
0:06 not just the static stuff that we've been playing with.
0:09 So I made a few changes to the web app, off screen,
0:12 just so we're ready and you don't have to watch me do a bunch of boilerplate stuff.
0:16 So I've created a new controller called albums,
0:19 of course put it in the controllers folder,
0:22 I've created a new template or views folder and I've added an index
0:26 in here, we'll look at that in a moment and see that we're rendering to that index,
0:30 through using that index page template as the renderer
0:34 right now we are returning nothing,
0:36 we are going to add something more interesting here in a moment,
0:38 I have also mapped this album controller to a set of URLs
0:45 and I've added this add_controller_routes method here,
0:48 kind of like I showed you in Talk Python,
0:51 over here I am saying - for any given controller
0:54 I would like to be able to map /that controller, /that controller, /albums, /albums/
1:02 and then /albums list and /albums/buy, as well as with some additional data.
1:08 OK, so this will work for almost every route that we are going to need
1:12 in our entire application, you'll see we've got a handful of other ones,
1:15 we have like a robots.txt and we want to have say some kind of sitemap,
1:21 maybe things like that don't fall well into this pattern,
1:24 but other than a few things like that we're totally good.
1:27 So we have this albums controller, this index method, which maps to just /albums, through this,
1:35 and we've got this basic code here, I've updated the fav icons
1:38 we are not using the default one anymore, we'll see that in a moment
1:41 and we've added some basic HTML to this page,
1:45 here is our little nav section up here and we just have these albums,
1:49 so we are going to come back and talk about this nav section in a moment,
1:53 for now let's talk about getting some interesting data over here,
1:57 so the other thing I've added is this services and data section to our site,
2:01 now you don't have to break it down like this
2:04 but I find on one hand you have data access layer, class definitions,
2:08 ORM information and all sorts of stuff that are like
2:12 right at the ORM layer mapping to the database,
2:15 and then you have one step above that some kind of data access logic
2:19 like I would always like to have my albums sorted like this
2:22 or any time I do a query against a user I want to join that against their purchases
2:28 so that does always efficient, and so to make that work more easily,
2:33 I defined services layer that then typically works with the ORM and the data definitions.
2:38 Now we don't have that yet, we haven't talked about databases,
2:41 so I am just going to return just some static models here
2:45 so we have one function get_albums, it returns a list of albums
2:48 and each album has a title, year, a preview image and a set of tracks.
2:52 Eventually, we'll put that into a couple of tables in the database
2:55 but for now let's just hack it together
2:58 the effect as far the template is concerned is basically the same.
3:02 So if we run our site, make sure everything is going,
3:05 we'll see here we've got a new link up here that I've added,
3:07 if we go there it just says albums, /albums, so that's great,
3:11 now let's pass the data, the idea is going to be that we would like to see
3:15 all the albums listed, and maybe even some information about their tracks.
3:18 Listed here so they'll have little picture and a link
3:22 and maybe some information about its track,
3:24 you'll notice that some of them have preview, some of them don't, so that's nice as well.
3:29 Alright, also here is the new icon, I've just made it this blue-yellow Python logo.
3:34 I don't need the Pyramid one anymore,
3:36 and of course this navigation is going to be replaced by a bootstrap navbar
3:39 but for now we'll just keep it there just to sort of help
3:42 with this common look and feel concept.