Python for Entrepreneurs Transcripts
Chapter: Build web apps with Pyramid: Applied web development
Lecture: Navigation based on logged in status and demo wrap up

Login or purchase this course to watch this video and the rest of the course contents.
0:01 Now, notice up here we have this home, albums, contact, right, as we click around, the other thing I'd like to do is
0:07 add just a little bit of a concept of a logged in user. We are going to get to user accounts and all that stuff later,
0:14 but let's start talking about the template a little bit up here. Now notice, right now I have this part hidden, but if I put it back,
0:21 we have this section that says should you sign in or register or are you already signed in, you can view account or log out.
0:29 And notice there is no real actions here but just from a template perspective, we would like to show some or the other, so let's go back over here,
0:36 our base controller here, and let's add some kind of behavior here, so we are going to have a property,
0:44 a property is basically a function that you interact with, like a field but in fact it's a function call, and it's also read-only, so we have this,
0:53 we'll say is_logged_in, and we are going to "return" right now, let's just "return True", yes, they are logged in.
1:02 So over here, let's go back for a moment, our base controller has all of these attributes, or fields
1:10 and then it has these properties, our albums controller derives from that so our albums controller has those properties as well,
1:18 so it has is_logged_in and so on, so that means over here I can do this, remember, when speaking of the controller we say view.,
1:28 so we'll say, it's tal condition, is_logged_in, actually, we don't want this, we want the opposite of that, so we'll say "not"
1:34 and then put this on those, we want this in the case you are logged in, in here we want to when you are not logged in.
1:43 So let's rerun it, because we changed the Python code which means we've got to restart the web app, so over here you can see we have our account,
1:50 we have our logout and that's because in the base controller we said yes, they are logged in, right now,
1:56 so if we come over here and look we have account, and log out, because those are logged in users, if this was false,
2:02 if they weren't logged in... we refresh this, rerun the code and refresh it, you can see now we have sign in and register,
2:08 so that's how we work with the template attribute language with Chameleon templates and so on, let's just review.
2:15 We go from our albums controller, we get to our action method, we do our data access, web service access, whatever calculations we want,
2:22 we return that as a dictionary, as a model, to the template. Great, so the template then has access to the view
2:30 which is the instance of the controller class and all of its properties and fields and so on,
2:35 so we can say "tal conditional" don't show this if they are logged in,
2:39 do show this if they are logged in, or show this if they're not logged in rather.
2:43 And then we come down here and we say create little prototypical HTML block that we want to work with and then we'll say "tal repeat"
2:52 and we'll create a local variable for each time through the loop
2:55 so here we are going to create - now that we are floating this we don't need this anymore, we are going to create an image
3:01 and we are going to use the image URL from the "a" each album coming through
3:05 again, we want to set the title, use the tal conditional on whether or not it has a preview
3:10 and then we are going to go the albums tracks and loop over those as well. And you can see that our albums are on the screen in a pretty decent way
3:20 that represents the data that is backing them. Like I said, we'll make this more beautiful later but it's a good start.


Talk Python's Mastodon Michael Kennedy's Mastodon