HTMX + Flask: Modern Python Web Apps, Hold the JavaScript Transcripts
Chapter: Feature 1: Click-to-edit
Lecture: Creating the new video in the database

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Well we got the data over to the server, we got the form, but we haven't added the video yet. All we did, we'd run this.
0:09 What we did is when you submit this data it just reads it, throws it away and then it goes back over here.
0:17 What we would like is to have another video that goes right there isn't it? Let's see if we can get some memory on these things.
0:25 A beautiful, beautiful. Here we go. It's got five or six views. Perfect. So now, when we submit this form we want to actually put it in
0:36 the database. So when it redirects here, it reloads that from the database which will have the new video on it. So how do we do that?
0:44 Well recall another section. Another bit of organization in the course here is this services and we don't have a whole lot going on here.
0:52 So it's pretty straightforward but this services thing, its job is all about managing the data, so we don't have to think about it
1:00 if we want to switch from one kind of data format to another, we could easily do that by just rewriting all_videos and all_categories and so on.
1:11 One of them though, check that out right there is really interesting, add_video. So all we need to do is go to this video_service and call add_video
1:19 and pass in this data like the category name, youtube_id, title and so on.
1:25 And guess what? again this is exactly the data that both our form and the URL together combined have been capturing,
1:34 you can go over here and just type video vs, and if we hit Ctrl + Tab a couple of times PyCharm will say oh
1:40 you want me to import this and use that one? Yes please. We want add_video And what does it take? It takes a category name which were passing in.
1:51 Right? see the parameter at the top and then the other ones that it needs come from this restore_from_form in our view model. So we have the id,
2:00 we're looking for the title, the author. and vm.view_count. We don't need our comment or TODO because this hopefully is
2:10 doing it. Let's give it a try, see how it works. So I have not pressed submit yet over here.
2:17 What we hope to see is that it's going to create the video in the database and then stash it right there.
2:22 Let's see, moment of truth. Of course, there it is. Of course. But as we click around we can come over here go back to
2:31 Python, still there, right? We put in the database and go play it. There's that guy again. Play this, fun,
2:40 right? Go watch this Python Bytes one, so on. That was pretty awesome, right? We were able to create this form,
2:48 create the code on the server that will capture it. Create the view model to make it clean, add it to the database.
2:56 Do a redirect, we've got everything created. Now, everything we've done so far has been only Flask code. There's zero htmx involved in this.
3:08 But why didn't I go through all the trouble? I could have just written this and thrown in the view model and done the HTML
3:12 But I really want you to appreciate this is what it takes to build it in Flask and here's how it's different to how we build it in htmx.
3:19 So what we're going to do now is we're going to go and change how this works, turns out most of what we've already done would be required anyway.
3:27 So we're going to change how this works to make it much nicer as we've wanted to from the beginning with htmx.
3:33 But we've got it working in Flask, for sure.

Talk Python's Mastodon Michael Kennedy's Mastodon