Building Data-Driven Web Apps with Pyramid and SQLAlchemy Transcripts
Chapter: User input and HTML forms
Lecture: Concept: One source of data
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
Let's review this concept
0:01
of having just one source of data
0:03
rather than four or five being passed in
0:05
from these different sources on request.
0:08
So we saw that if we just create just a few lines of code
0:11
the simple create function that takes your request
0:14
and returns this thing we created
0:15
called a request dictionary
0:17
which is really a fancy dictionary
0:19
that lets you do sort of arbitrary dynamic access
0:22
into its keys as if they were fields or attributes
0:25
rather than dictionary style with .get
0:28
or square brackets and things like that.
0:30
Then what we're going to do is we're going to
0:32
pass in the request and we're going to say
0:33
"Let's go and get the query string
0:35
and the headers and the Post and the matchdict
0:38
and all of that stuff and merge it
0:39
into one super dictionary."
0:41
And if there's data that is in both
0:43
like say in the matchdict and the Get
0:46
the last one wins.
0:47
So in this case anything in matchdict
0:49
is going to override anything in the query string.
0:52
And then if we just use this in our action methods
0:55
it gets so much simpler.
0:56
So over here in our registered post
0:59
we say create us one of these dictionaries
1:01
when we say d.email_address d.password
1:03
where email_address and password
1:06
these were the named input elements in our form.
1:10
This is certainly an optional design pattern
1:12
but it's very very useful to help just centralize
1:16
where all this data is coming from.