Python for Entrepreneurs Transcripts
Chapter: Build web apps with Pyramid: Applied web development
Lecture: Concept: View models
Login or
purchase this course
to watch this video and the rest of the course contents.
0:03
Now that you've seen view models in action, let's review them in a picture. So we've seen that we can have these view models, classes really,
0:12
in this case we have a RegistrationViewModel that we've created and what we want to do is use this type to help alleviate some of the challenges
0:19
of sharing and passing data back to the views as well as validation and getting it from forms. So we are going to have two methods here, two actions,
0:29
one is a register, which is handling the GET request and one, which is going to handle the POST or the form submission.
0:35
Here what we need to do in this register_get is we need to return the data that will be displayed in the Chameleon template,
0:42
so we probably have to have an email and a password and things like that. Our RegistrationViewModel is a class that has those properties or fields
0:51
and we write a to_dict method that will turn that to a dictionary, basically a model as far as Pyramid is concerned, to be passed back.
0:59
So, this is really nice and simple and this might even be doing data access in the constructor for the registration view model, right,
1:06
it could be doing lots of interesting stuff to get that data, that model prepared. Next, we want to handle the form POST,
1:13
now we have the register_post and how do we do this? Again we are going ot create our view model, and now we have a from_dict,
1:19
so given a dictionary which in this case is coming from the form POST, let's parse that data back into our variables,
1:26
in our registration view model and then we'll be able to work with all those values as if they were just present in the view model.
1:33
We also kind of add validation, so here we can call a validate method once we've restored it from the dictionary, we can check
1:40
and even use this error field that we've added, so if it happens to be there is something wrong, we don't want to accept their form POST,
1:48
in this case we don't want to let them register for the site, we can just return the view model to the same template and this will round trip
1:54
all of their data they have entered as well, so this is really nice and simple.
1:59
If we do validate, then we'll work with the various fields on that view model, so view model email address, view model password and so on,
2:07
once we actually create the account and do the login and so on, then we might raise an HTTP found exception
2:13
to redirect the user to wherever they need to go. Remember, the GET/POST/redirect pattern. Here it is, with view models.
2:22
Let's look into this to/from dictionary concept a little more deeply. So what we've done is we've realized
2:29
that basically any view model needs to be able to convert itself to a dictionary so it can be passed to the templates,
2:35
so we've created a ViewModelBase so that it has the simplest possible implementation of "to dictionary".
2:39
it just returns all the fields, if for some reason your particular view model can't work this way,
2:45
just overwrite this method and create a dictionary and return it. Now we have our various page-specific view models like a registration view model
2:52
and it derives from this ViewModelBase and it has a __init__ where it creates this fields, so above when we say "to dictionary", we get
2:59
an email address, a password, and an error passed to the view. Optionally, if it's supposed to support a processing of form post,
3:07
we can add it from_dict as well, not all of view models need this, but this particular one did. And so here you can see we are passing in a dictionary
3:17
we don't really care where it came from, and the case we just saw it from the form POST but it could come from the variety of locations
3:22
and we are going to use the GET sort of the safe dictionary key access
3:26
or dictionary value access to get the email address and password and store those.