Skip to content

Commit be79705

Browse files
committed
fixing django admin redirect for django v4.8.0 and above
1 parent 8e1ed07 commit be79705

File tree

3 files changed

+42
-2
lines changed

3 files changed

+42
-2
lines changed

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ jobs:
1414
- name: Set up Python
1515
uses: actions/setup-python@v5
1616
with:
17-
python-version: 3.8
17+
python-version: 3.10
1818
- name: Install Flit
1919
run: pip install flit
2020
- name: Install Dependencies

ellar_django/middleware.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import typing as t
2+
3+
from ellar.common.types import ASGIApp, TMessage, TReceive, TScope, TSend
4+
from starlette.datastructures import MutableHeaders
5+
6+
7+
class DjangoAdminRedirectMiddleware:
8+
def __init__(self, app: ASGIApp, path_prefix: str) -> None:
9+
self.app = app
10+
self.path_prefix = path_prefix
11+
12+
async def __call__(self, scope: TScope, receive: TReceive, send: TSend) -> t.Any:
13+
if scope["type"] != "http":
14+
return await self.app(scope, receive, send)
15+
16+
async def sender(message: TMessage) -> None:
17+
if message["type"] == "http.response.start":
18+
headers = message.get("headers")
19+
if headers:
20+
working_headers = MutableHeaders(
21+
raw=[
22+
(key.decode("latin-1").lower().encode("latin-1"), value)
23+
for key, value in headers
24+
]
25+
)
26+
location = working_headers.get("location")
27+
if location and not location.startswith(self.path_prefix):
28+
working_headers["Location"] = (
29+
f"{self.path_prefix}{working_headers['Location']}"
30+
)
31+
message["headers"] = working_headers.raw
32+
await send(message)
33+
34+
return await self.app(scope, receive, sender)

ellar_django/module.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from starlette.routing import Mount
99

1010
from .commands import django_command_wrapper
11+
from .middleware import DjangoAdminRedirectMiddleware
1112

1213
_router = ModuleRouter()
1314

@@ -28,7 +29,12 @@ def setup(cls, settings_module: str, path_prefix: str = "/dj") -> "DynamicModule
2829
path_prefix,
2930
routes=[
3031
_router_as_mount,
31-
Mount("/", app=get_asgi_application()),
32+
Mount(
33+
"/",
34+
app=DjangoAdminRedirectMiddleware(
35+
get_asgi_application(), path_prefix
36+
),
37+
),
3238
],
3339
)
3440
return DynamicModule(cls, routers=[mount])

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy