Python for Entrepreneurs Transcripts
Chapter: Build web apps with Pyramid: Part 1
Lecture: Building Block: Views
0:01 Let's look at the views we get when we create a brand new app from the scaffold in Pyramid.
0:06 Now, this is not a very realistic one but it lets us point out the key elements and we'll look at a more realistic one afterwards.
0:12 It all starts by importing the view_config decorator from pyramid.view and here we can specify the route name as well as the template
0:20 that will be rendered from this view. Next, we define a method, the method name doesn't have to correspond anything
0:27 in the view configuration or anything like this, it just happens to be some kind of method that takes a request object,
0:34 it doesn't have to do anything with it but it does need to have the right structure that is going to do some sort of processing,
0:39 here we just create a dictionary and pass some static data so, we are going to create a model as a dictionary, this one has a project key in it,
0:47 we are going to pass it to mytemplate.pt, this is a Chameleon template and somewhere it presumably is working
0:52 with the value of project so it will display web app somewhere. Like I said, that previous view, not so realistic.
0:59 Let's look at maybe something more realistic here, so what this method is, this is going to be a "reset your password" method
1:07 and note we've given it the route_name of reset_post, you can see on the next line it's only accepting form posts.
1:14 If you do a straight request against this URL, it'll just say "I can't find this for you", but if you do an HTTP post against it,
1:21 it will say I am actually in the process of resetting my password, here is the form data, well it will be happy to process it.
1:27 Notice it also has a template and the template name corresponds to the template method,
1:32 this is not required like I said but for sanity sake, I would recommend it. So what does it look like to handle this request?
1:39 Well, it begins with going to our request it has a POST and a get dictionary and we are going to go to the POST dictionary
1:47 and get the email that was submitted in the form. next, we are going to go to our data layer and create a password reset request,
1:54 we'll pass over the email and presumably in here we'll also check in if the email is empty, or doesn't exist
1:59 or it's just going to return an error and say "we cannot reset your password", Notice the reset_password.pt view will be used to show the error
2:06 and presumably that's the same as the get version that they filled out the form to say I need to reset my password and here is my email.
2:13 If everything goes OK, we'll do something like send them an email saying "here is the link" and then finally we'll send them back a message that says
2:21 "check your email for the reset code." This is what a realistic view looks like in Pyramid
2:26 and you will write probably hundreds of them, building your application.