Skip to content

Commit 5591683

Browse files
authored
Merge pull request #17 from pauldiepold/csrf-for-non-inertia-requests
CSRF for non inertia requests
2 parents e521b32 + 829c269 commit 5591683

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

inertia/middleware.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ def __init__(self, get_response):
1010
def __call__(self, request):
1111
response = self.get_response(request)
1212

13-
if not self.is_inertia_request(request):
14-
return response
15-
1613
# Inertia requests don't ever render templates, so they skip the typical Django
1714
# CSRF path. We'll manually add a CSRF token for every request here.
1815
get_token(request)
1916

17+
if not self.is_inertia_request(request):
18+
return response
19+
2020
if self.is_non_post_redirect(request, response):
2121
response.status_code = 303
2222

inertia/tests/test_rendering.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,3 +104,8 @@ def test_that_csrf_inclusion_is_automatic(self):
104104
response = self.inertia.get('/props/')
105105

106106
self.assertIsNotNone(response.cookies.get('csrftoken'))
107+
108+
def test_that_csrf_is_included_even_on_initial_page_load(self):
109+
response = self.client.get('/props/')
110+
111+
self.assertIsNotNone(response.cookies.get('csrftoken'))

0 commit comments

Comments
 (0)