blue-toothbrush-6988
01/20/2023, 11:11 AMblue-toothbrush-6988
01/20/2023, 11:12 AMadventurous-ocean-93733
01/20/2023, 11:12 AMhx-boost="true"
if you're not alreadyadventurous-ocean-93733
01/20/2023, 11:12 AMadventurous-ocean-93733
01/20/2023, 11:14 AMhx-target
it will swap itself with the new content.blue-toothbrush-6988
01/20/2023, 11:14 AMblue-toothbrush-6988
01/20/2023, 11:14 AMblue-toothbrush-6988
01/20/2023, 11:16 AMpurple-easter-43922
01/20/2023, 12:16 PM401: Unauthorized
with some text content.purple-easter-43922
01/20/2023, 12:16 PMadventurous-ocean-93733
01/20/2023, 12:56 PMpurple-easter-43922
01/20/2023, 1:54 PMripe-crowd-36857
01/20/2023, 2:01 PMpurple-easter-43922
01/20/2023, 2:06 PMripe-crowd-36857
01/20/2023, 2:07 PMpurple-easter-43922
01/20/2023, 2:08 PM{% extends "base.html" %}
at the top).adventurous-ocean-93733
01/20/2023, 2:12 PMdocument.body.addEventListener('htmx:beforeSwap', function(evt) {
if(evt.detail.xhr.status === 404){
// alert the user when a 404 occurs (maybe use a nicer mechanism than alert())
alert("Error: Could Not Find Resource");
} else if(evt.detail.xhr.status === 422){
// allow 422 responses to swap as we are using this as a signal that
// a form was submitted with bad data and want to rerender with the
// errors
//
// set isError to false to avoid error logging in console
evt.detail.shouldSwap = true;
evt.detail.isError = false;
} else if(evt.detail.xhr.status === 418){
// if the response code 418 (I'm a teapot) is returned, retarget the
// content of the response to the element with the id `teapot`
evt.detail.shouldSwap = true;
evt.detail.target = htmx.find("#teapot");
}
});
From https://htmx.org/docs/#modifying_swapping_behavior_with_eventsadventurous-ocean-93733
01/20/2023, 2:13 PMripe-crowd-36857
01/20/2023, 2:13 PMlogin.html
could look like this
{% extends "base.html" %}
<h1>Log in</h1>
{% include "form" %}
form.html
would look
<form>
{% if errors %}
<p>Incorrect password or username</p>
{% endif %}
<input type="text" name="username" />
<input type="password" name="password" />
<input type="submit" />
</form>
now in your controller, you can check for that HX-Request
request header and then either render login.html
or just form.html
purple-easter-43922
01/20/2023, 2:15 PM<script>
tag? I've gotten used to "local" JavaScript from Svelte, and it would convenient if the JS could live in the partial.purple-easter-43922
01/20/2023, 2:16 PMadventurous-ocean-93733
01/20/2023, 2:17 PMmysterious-toddler-20573
01/20/2023, 3:24 PMvalue
a possible value for hx-target
. In fact, perhaps you need to do that.adventurous-ocean-93733
01/20/2023, 3:24 PMmysterious-toddler-20573
01/20/2023, 3:24 PMclean-midnight-60710
01/20/2023, 6:25 PMmysterious-toddler-20573
01/20/2023, 6:33 PMfreezing-sugar-15438
01/20/2023, 8:56 PMmysterious-toddler-20573
01/20/2023, 8:57 PMmysterious-toddler-20573
01/20/2023, 8:57 PM