HTMX + Flask: Modern Python Web Apps, Hold the JavaScript Transcripts
Chapter: Course conclusion and review
Lecture: Partial or full response?

Login or purchase this course to watch this video and the rest of the course contents.
0:00 We saw that sometimes when you make a request you're loading up the whole page and other times you want maybe just a fragment of that page.
0:08 The example where this was happening was in the search and specifically for deep linking.
0:15 So if you're loading the page but you've got the search text because we've put that in the URL and you're clicking or loading up that link.
0:23 You need to do all the stuff of showing the outer page but also loading up that video search and putting that in the results of the page.
0:31 On the other hand if someone's typing you need to go back and do a search and render just the partial bit and put it into that section.
0:39 It turns out that we can do that with a single function. You may or may not want to do this,
0:44 it might be simpler to have them separated, or it might be simpler to have it
0:48 together. My current thinking is that if you're doing deep linking it might make sense
0:52 to have it all in one, if you're not doing deep linking like for example the infinite scroll. Nobody's deep linking to that.
1:00 In our example, in the click to edit no one's deep linking to that.
1:03 So those were separate but this search one did support deep linking, and so I thought it made sense to put it together.
1:08 Well how do you know which it is, a request coming into /videos/search could be from htmx, it could be from a browser directly trying to load the page.
1:18 We added a feature to our view model that says is_htmx_request. How does that work? Recall that all that happens when htmx makes a request, every time
1:30 it puts some header value that says HX-Request. That's the key thing you're going to look up, and it has some
1:37 value but we don't actually care what the value is. We just say if this is present in the headers, this means that htmx very likely put it there.
1:45 We're going to assume it's an htmx request. So this simple bit of code HX-Request in flask.request.headers
1:52 is really all you need to do to check to see if it's a full page request or some callback from htmx.


Talk Python's Mastodon Michael Kennedy's Mastodon