Building Data-Driven Web Apps with Pyramid and SQLAlchemy Transcripts
Chapter: Introducing the Pyramid framework
Lecture: Building block: Views
0:00 So, let's dig into these building blocks.
0:02 First of all, we have our views or controllers.
0:06 So, what does that look like?
0:07 Well, we start by importing the necessary decorator,
0:11 from Pyramids, so from pyramid.view import view_config,
0:15 and then we just define a function.
0:17 The name of the function doesn't actually matter,
0:20 in this case, and then we're going
0:22 to map... a route... to that view.
0:26 In order to make a regular function
0:29 become one of these controller functions, or actions,
0:32 what we're going to have to do is
0:33 we put this @view_config decorator.
0:36 Usually what you'll find is there's at least
0:38 two values you set, sometimes it can be omitted,
0:41 sometimes there's more, we'll talk about that...
0:44 Is we're going to set the route name.
0:46 This is what URL maps to this function, it's a route name.
0:51 And then the template that we're going to use
0:54 to generate the dynamic HTML, so that's the renderer,
0:57 we're going to use mytemplate.pt right now.
1:00 And then this method always takes a request object.
1:04 Which is one parameter request, and that's the way it works.
1:07 The request carries with it things like the data passed,
1:10 and the URL, the form post, all that kind of stuff.
1:13 It's sort of the jumping off point to get the data out of
1:16 all the inbound stuff, and things like cookies, and so on.
1:20 We're going to take that data, do whatever it is we got to do,
1:23 maybe check for an account,
1:24 maybe pull some stuff from the database, things like that.
1:27 And then when we're ready we're going to
1:28 return our model to the template.
1:31 We do this by returning our model as a dictionary
1:34 and the framework is going to manage getting that
1:37 moved over to the template and rendered dynamically
1:40 and responded back as HTML to the browser.
1:45 This is about as simple as things get, with views right?
1:49 All we do is we have a function,
1:50 it returns a static dictionary.
1:52 Let's look at a more realistic one.
1:55 Over here, we have one and we're saying
1:57 this actually only responds to POST messages.
2:02 We're going to reset password and maybe there's one that
2:04 generates the form but this one is going to do
2:06 the password reset when they submit that form, okay?
2:10 And first of all we're going to get the data
2:13 that they put into the form, right?
2:14 What is your email that you're going
2:16 to reset your password to when they click the button?
2:19 All that data from the form
2:21 ends up in a dictionary called POST.
2:23 Alright, we're going to grab that out...
2:26 And then we're going to try to create
2:27 a password reset using our data access layer.
2:31 That may or may not work, but if it doesn't work,
2:33 then we're going to send them an error saying,
2:35 "No no, sorry that didn't work,"
2:37 and we're going to render the reset password again,
2:40 but this time the error message will be shown.
2:43 And if it does work then we're going to say,
2:46 "Alright well it looks like that worked,"
2:47 so send them a message that says,
2:49 "Check your email for the reset code."
2:52 Presumably that repository thing actually sent the email.
2:54 Not sure that's a great pattern
2:56 but it fits on one slide, so that's why it looks like that.
2:59 Alright, the idea is we call that function,
3:01 and either it did not send the password or send the email,
3:04 or if it did it saved it in the database, sent the email,
3:07 and now they just got to go check their email.
3:09 Alright this is a more realistic type
3:11 of view here processing a form POST.