-
-
Notifications
You must be signed in to change notification settings - Fork 139
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
Conversation
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? |
I'm not sure we want a class for this? Most 200's will be generated by response = render(...)
return trigger_client_navigation(response, "/somewhere-new/", target="main") |
src/django_htmx/http.py
Outdated
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) |
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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
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. |
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. |
ee4a147
to
08f704a
Compare
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? |
Not with postponed evaluation.
Indeed! |
7a0efc8
to
33454d5
Compare
Actually probably better to take explicit arguments. |
TY - will update in the next few days |
HttpResponseLocation
to send response with HX-Location
33454d5
to
f59cc75
Compare
I looked at this again and realized that if you send Rather than use a I added docs and a changelog note, and now I think it’s ready to merge. Thank you for your contribution. |
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 |
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.