[Django]-Adding javascript to a Django redirect (HttpResponse)

3👍

You can use js appproach window.location

url = 'someUrl'
resp_body = '<script>alert("You must remove an item before adding another");\
             window.location="%s"</script>' % url
return HttpResponse(resp_body)
👤deedee

2👍

You are getting confused between frontend and backend. When this view is requested by the user, the server (via Django’s HTTPResponseRedirect) is sending a 302 redirect HTTP response that tells the browser to redirect to a different page. This is different to a normal 200 response (i.e. a normal Django response object, not a redirect) within which you could include a javascript alert.

You need to show that javascript on the view you are initially loading before the user is returned the redirect response.

Leave a comment