JSONResponse / Redirect help

Using Django Functional Views, JS form handling and sending to an API view in Django. Plain JS not React or anything.

I’m sending an object that my functional view is parsing and saving to a database. Got all that working. When I send a “return JsonResponse” everything’s hunky-dory. I tried to get fancy and write a redirect OR a return(render) of the page I want to go to with a context dict, including the JsonResponse inside the Dict. This kept throwing errors AND not redirecting. I’ve tried sending the JsonResponse the response msg and the redir path and sending the redirect through the django view, but one side always mucks up. Right now I’m only able to save the form using the API call and return the JsonResponse in the view. I can’t redirect as well.

I’d prefer to redirect using Django as opposed to JS but whatever’s cleaner. Can post my code as well just figured someone here had to have dealt with this by now and Django’s documentation + YouTube are unclear.

TL;DR- JS needs a JsonResponse back, but I want to redirect thru Django AND send the JsonResponse. Easy solution incoming? StackOverflow claims I need to pass the JsonResponse an object w the JsonResponse(‘successmessage’: x, ‘redirectpath’ redirectpath) etc. but i’m encountering errors. What’s best practice/worked for yall ?

Thanks for your help.

You’re going to need to provide more specific details here, like the view that you have written and the JavaScript making the request.

Keep in mind that a Django redirect results (generally) in a 302 being returned. It’s up to the JavaScript to recognize the 302 response and request the new page. Therefore, unless your JS is prepared to handle a 302 with a response body, any data should be returned by the redirected page.