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 replace_url() #396

Merged
merged 3 commits into from
Aug 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
Changelog
=========

* Add :func:`django_htmx.http.replace_url()` for setting the ``HX-Replace-URL`` header.

Thanks to Bogumil Schube in `PR #396 <https://github.com/adamchainz/django-htmx/pull/396>`__.

* Add ``select`` parameter to ``HttpResponseLocation``.

Thanks to Nikola Anović in `PR #462 <https://github.com/adamchainz/django-htmx/pull/462>`__.
Expand Down
28 changes: 28 additions & 0 deletions docs/http.rst
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,34 @@ Response modifying functions
return push_url(response, f"/branch/{leaf.branch.id}")
...

.. autofunction:: replace_url

Set the |HX-Replace-Url header|__ of ``response`` and return it.
This header causes htmx to replace the current URL in the browser location history.

.. |HX-Replace-Url header| replace:: ``HX-Replace-Url`` header
__ https://htmx.org/headers/hx-replace-url/

:param response:
The response to modify and return.

:param url:
The (relative) URL to replace, or ``False`` to prevent the location history from being updated.

For example:

.. code-block:: python

from django_htmx.http import replace_url


def dashboard(request):
...
response = render(request, "dashboard.html", ...)
# Pretend the user was always on the dashboard, rather than wherever
# they were on before.
return replace_url(response, "/dashboard/")

.. autofunction:: reswap

Set the |HX-Reswap header|__ of ``response`` and return it.
Expand Down
5 changes: 5 additions & 0 deletions src/django_htmx/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,11 @@ def push_url(response: _HttpResponse, url: str | Literal[False]) -> _HttpRespons
return response


def replace_url(response: _HttpResponse, url: str | Literal[False]) -> _HttpResponse:
response["HX-Replace-Url"] = "false" if url is False else url
return response


def reswap(response: _HttpResponse, method: str) -> _HttpResponse:
response["HX-Reswap"] = method
return response
Expand Down
19 changes: 19 additions & 0 deletions tests/test_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from django_htmx.http import HttpResponseLocation
from django_htmx.http import HttpResponseStopPolling
from django_htmx.http import push_url
from django_htmx.http import replace_url
from django_htmx.http import reswap
from django_htmx.http import retarget
from django_htmx.http import trigger_client_event
Expand Down Expand Up @@ -107,6 +108,24 @@ def test_success_false(self):
assert response["HX-Push-Url"] == "false"


class ReplaceUrlTests(SimpleTestCase):
def test_success(self):
response = HttpResponse()

response2 = replace_url(response, "/index.html")

assert response2 is response
assert response["HX-Replace-Url"] == "/index.html"

def test_success_false(self):
response = HttpResponse()

response2 = replace_url(response, False)

assert response2 is response
assert response["HX-Replace-Url"] == "false"


class ReswapTests(SimpleTestCase):
def test_success(self):
response = HttpResponse()
Expand Down
Loading