Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add HttpResponseLocation to send response with HX-Location #239

Merged
merged 9 commits into from
Nov 10, 2022

Conversation

gone
Copy link
Contributor

@gone gone commented Jun 10, 2022

Adding a new http response to use client side redirects: bigskysoftware/htmx#832

I think the most common pattern is:

Form fails validation, needs to be loaded in the same location as it was previously displayed OR it's successful and then you want to redirect on the client side without destroying client side state.

@gone
Copy link
Contributor Author

gone commented Jun 10, 2022

I just wanted to get this up for initial review - I also need to create documentation for this.

Any other tweaks you want for this? A sample case in the demo app?

@adamchainz
Copy link
Owner

I'm not sure we want a class for this? Most 200's will be generated by TemplateResponse via e.g. render(). All we have to do is add some headers on top to make it a "htmx location response". I imagine this to be more convenient as a function, similar to trigger_client_event():

response = render(...)
return trigger_client_navigation(response, "/somewhere-new/", target="main")

Comment on lines 31 to 45
swap_spec_kwargs = (
"source",
"event",
"handler",
"target",
"swap",
"values",
"headers",
)

def __init__(self, redirect_to: str, *args: Any, **kwargs: Any) -> None:
swap_details = {}
for key in self.swap_spec_kwargs:
if key in kwargs:
swap_details[key] = kwargs.pop(key)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(rather than take arbitrary kwargs and then extract a limited set, declare arguments explicitly, so that type hints can help users define them correctly)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did that and found myself using Any for a couple of them - I need to double check headers/values to remember what that should be

@gone
Copy link
Contributor Author

gone commented Jun 11, 2022

My intention was returning this from a successful form submission instead of the standard redirect - that case wouldn't be great as function since you'd need to call it at the template view, and not the form_successful of the form view.

Are you thinking that the function version would be useful as a way to chain requests? Like load this template AND load this second template in this section over here? EG you've logged in so load the dashboard page AND load the new logged in header in a second request. I hadn't considered that case but it seems powerful.

@adamchainz
Copy link
Owner

I think the tools we provide here should be general purpose. If we make this a function, users can generate an HTML response (even streaming!) and attach the appropriate headers to tell htmx how to swap in the page. If add a response class, it can only be used in one way.

@gone gone force-pushed the feature/hx_location branch from ee4a147 to 08f704a Compare June 22, 2022 19:05
@gone
Copy link
Contributor Author

gone commented Jun 22, 2022

Updated to use a function to set the parameters

I'm pretty new to typing; I wrote the typing with Optional[..] and the precommit pulled that out and replaced with the | syntax. Won't that break on python 3.7? Do you think there's value in creating a typed dict for the swapspec?

@adamchainz
Copy link
Owner

I'm pretty new to typing; I wrote the typing with Optional[..] and the precommit pulled that out and replaced with the | syntax. Won't that break on python 3.7?

Not with postponed evaluation.

Do you think there's value in creating a typed dict for the swapspec?

Indeed!

@adamchainz
Copy link
Owner

Do you think there's value in creating a typed dict for the swapspec?

Actually probably better to take explicit arguments.

@gone
Copy link
Contributor Author

gone commented Sep 13, 2022

TY - will update in the next few days

@adamchainz adamchainz changed the title Adding in hx-location response Add HttpResponseLocation to send response with HX-Location Nov 10, 2022
@adamchainz
Copy link
Owner

I looked at this again and realized that if you send HX-Location, htmx stops processing the rest of the response. Therefore there wasn’t actually much value in set_location, so I removed it.

Rather than use a TypedDict, I expanded to use individual arguments. In the process I noted that handler doesn’t actually work: bigskysoftware/htmx#1129 , so I didn’t implement support for it here.

I added docs and a changelog note, and now I think it’s ready to merge. Thank you for your contribution.

@adamchainz adamchainz merged commit 04d89c1 into adamchainz:main Nov 10, 2022
@gone
Copy link
Contributor Author

gone commented Jan 4, 2023

Thanks for finishing up - sorry I let this one drop. Let me look at the error in htmx and see if I can fix that

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants