Python for Entrepreneurs Transcripts
Chapter: Build web apps with Pyramid: Applied web development
Lecture: Concept:Template introduction

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

Talk Python's Mastodon Michael Kennedy's Mastodon