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
0:04
other times you want maybe just a fragment of that page.
0:07
The example where this was happening was in the search and specifically for deep linking.
0:14
So if you're loading the page but you've got the search text because we've put that
0:18
in the URL
0:19
and you're clicking or loading up that link.
0:22
You need to do all the stuff of showing the outer page but also loading up
0:26
that video search and putting that in the results of the page.
0:30
On the other hand if someone's typing you need to go back and do a search
0:34
and render just the partial bit and put it into that section.
0:38
It turns out that we can do that with a single function.
0:41
You may or may not want to do this,
0:43
it might be simpler to have them separated, or it might be simpler to have it
0:47
together. My current thinking is that if you're doing deep linking it might make sense
0:51
to have it all in one, if you're not doing deep linking like for example the
0:56
infinite scroll. Nobody's deep linking to that.
0:59
In our example, in the click to edit no one's deep linking to that.
1:02
So those were separate but this search one did support deep linking, and so I thought it
1:06
made sense to put it together.
1:07
Well how do you know which it is, a request coming into /videos/search
1:12
could be from htmx,
1:14
it could be from a browser directly trying to load the page.
1:17
We added a feature to our view model that says is_htmx_request.
1:23
How does that work? Recall that all that happens when htmx makes a request, every time
1:29
it puts some header value that says HX-Request.
1:32
That's the key thing you're going to look up, and it has some
1:36
value but we don't actually care what the value is.
1:38
We just say if this is present in the headers,
1:41
this means that htmx very likely put it there.
1:44
We're going to assume it's an htmx request.
1:47
So this simple bit of code HX-Request in flask.request.headers
1:51
is really all you need to do to check to see if it's
1:54
a full page request or some callback from htmx.