Skip to content
This repository was archived by the owner on Dec 11, 2024. It is now read-only.

Commit e070cf2

Browse files
authored
Merge pull request #8 from girardinsamuel/feat/location
Add external redirects feature
2 parents 852fd7a + e1a5077 commit e070cf2

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

inertia/views.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import json
22
from inspect import signature
33
from django.core.exceptions import ImproperlyConfigured
4+
from django.http.response import HttpResponse, HttpResponseRedirect
45
from django.views.generic import View
56
from django.views.generic.list import BaseListView
67
from django.views.generic.detail import BaseDetailView
@@ -117,6 +118,15 @@ def render_inertia(request, component_name, props=None, template_name=None):
117118
url=request.get_full_path())
118119
return render(request, inertia_template, context)
119120

121+
122+
def location(url):
123+
"""Redirect to an external website, or even another non-Inertia endpoint in your app,
124+
within an Inertia request."""
125+
response = HttpResponse(status=409)
126+
response["X-Inertia-Location"] = url
127+
return response
128+
129+
120130
class InertiaMixin:
121131
component_name = ""
122132
props = None

test.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
)
2222
django.setup()
2323
from inertia.version import get_version
24-
from inertia.views import render_inertia
24+
from inertia.views import location, render_inertia
2525
from inertia.middleware import InertiaMiddleware
2626

2727

@@ -126,3 +126,9 @@ def lazy_loaded_prop():
126126
response = render_inertia(request, "Index", {"a": "1", "b": lazy_loaded_prop})
127127
# check that b is not returned because we only ask for a
128128
self.assertIn(b'"props": {"a": "1"},', response.content)
129+
130+
131+
def test_location(self):
132+
response = location("https://github.com")
133+
self.assertEqual(409, response.status_code)
134+
self.assertEqual(('X-Inertia-Location', 'https://github.com'), response._headers['x-inertia-location'])

0 commit comments

Comments
 (0)