-
-
Notifications
You must be signed in to change notification settings - Fork 140
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 redirect_middleware for handling htmx request redirects #406
Changes from all commits
3afb6ca
cec2354
ebecc04
32c9ac5
b8e3910
b7bea62
7b26052
7b1e325
6e1cb9f
4b24eac
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,11 +5,13 @@ | |
from typing import Awaitable | ||
from typing import Callable | ||
from urllib.parse import unquote | ||
from urllib.parse import urlparse | ||
from urllib.parse import urlsplit | ||
from urllib.parse import urlunsplit | ||
|
||
from asgiref.sync import iscoroutinefunction | ||
from asgiref.sync import markcoroutinefunction | ||
from django.conf import settings | ||
from django.http import HttpRequest | ||
from django.http.response import HttpResponseBase | ||
from django.utils.functional import cached_property | ||
|
@@ -112,3 +114,21 @@ def triggering_event(self) -> Any: | |
except json.JSONDecodeError: | ||
value = None | ||
return value | ||
|
||
|
||
def redirect_middleware(get_response): | ||
"""Middleware to handle redirects for htmx requests for login required views""" | ||
|
||
def middleware(request): | ||
response = get_response(request) | ||
if request.headers.get("HX-Request") and response.status_code == 302: | ||
location = urlparse(response["Location"]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fyi |
||
if location.path == settings.LOGIN_URL: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
redirect_url = ( | ||
f"{settings.LOGIN_URL}?next={settings.LOGIN_REDIRECT_URL}" | ||
) | ||
Comment on lines
+127
to
+129
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We'd wnat to preserve |
||
response["HX-Redirect"] = redirect_url | ||
response.status_code = 204 | ||
return response | ||
|
||
return middleware |
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.
copy-pasting the source code into the docs isn't necessary. they would drift over time.
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 think you're right, it does seem to be project-specific what should be done in cases like these. Thanks for taking the time to review!