HTMX + Flask: Modern Python Web Apps, Hold the JavaScript Transcripts
Chapter: Feature 1: Click-to-edit
Lecture: Concept: Processing the form submission on the server

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Final thing that we need to talk about and rounding out this feature we've added is actually processing the form submission on the server.
0:09 So when they go, and they fill out the form, they hit create new video. It's going to do a form submission and do a
0:15 POST back to the server. It's going to run this method add_post and notice our blueprint is not doing a blueprint.get or blueprint.route, it's
0:24 doing a blueprint.post. So we'll only listen to the POST HTTP verb. And we have our add_post method. It's getting the category name sent into the URL
0:32 And we're going to create our view model. Remember the job of the view model is to very carefully understand, validate,
0:39 transform the data exchange between the HTML page in the form and what gets submitted on the server. Exactly, what we need here.
0:46 We create our view model. We give it the parameters that are passed in. We say restore yourself from the form and that says oh I think the form is
0:54 going to pass me a Youtube ID, and a title and a view_count. No, I also have to convert the view_count to an integer and use a reasonable
1:01 default value and stuff like that. So it's going to parse all that data and get it ready for us to work with. We want to add it to the database,
1:08 so we're going to use our video service.add_video, pass category name, vm.id, title,
1:13 author and view_count. And then we're just going to reload that category page by
1:18 simply redirecting back to it. But now when we reload the page it will have new data in the database, it will refresh.
1:24 So we're gonna do a flask.redirect over to videos/category/ cat_name and that does it. This is it. This is all we had to do to save the video to
1:33 the database and validate it and so on, on the server.


Talk Python's Mastodon Michael Kennedy's Mastodon