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
0:03 is actually processing the form submission on the server.
0:08 So when they go, and they fill out the form,
0:10 they hit create new video. It's going to do a form submission and do a
0:14 POST back to the server. It's going to run this method add_post and notice
0:19 our blueprint is not doing a blueprint.get or blueprint.route, it's
0:23 doing a blueprint.post. So we'll only listen to the POST HTTP verb.
0:28 And we have our add_post method.
0:29 It's getting the category name sent into the URL
0:31 And we're going to create our view model.
0:34 Remember the job of the view model is to very carefully understand, validate,
0:38 transform the data exchange between the HTML page in the form and what gets submitted on
0:43 the server. Exactly, what we need here.
0:45 We create our view model. We give it the parameters that are passed in.
0:49 We say restore yourself from the form and that says oh I think the form is
0:53 going to pass me a Youtube ID,
0:55 and a title and a view_count.
0:56 No, I also have to convert the view_count to an integer and use a reasonable
1:00 default value and stuff like that.
1:02 So it's going to parse all that data and get it ready for us to work with.
1:05 We want to add it to the database,
1:07 so we're going to use our video service.add_video,
1:09 pass category name, vm.id, title,
1:12 author and view_count. And then we're just going to reload that category page by
1:17 simply redirecting back to it. But now when we reload the page it will have
1:22 new data in the database, it will refresh.
1:23 So we're gonna do a flask.redirect
1:24 over to videos/category/
1:27 cat_name and that does it.
1:29 This is it. This is all we had to do to save the video to
1:32 the database and validate it and so on, on the server.