2

Here's my scenario: User adds something to the shopping cart, and if the python view handling this logic does it successfully, it should fire off a JavaScript function saying 'You've added a product into your cart!'.

How would one go about doing that?

Here's what I tried so far:

return HttpResponse("alert('hello world');", mimetype="application/x-javascript")

but this just outputs the code as a string.

To be specific: I need to fire a function in a JS file.

1 Answer 1

1

In django 1.9 the following code works:

return HttpResponse("<script>alert('hello world');</script>")
Sign up to request clarification or add additional context in comments.

5 Comments

This works, but I need to fire a specific function in a JS file. Any tips?
If your javascript file is included in the response you can just call the function in the script tags above. If i were you I would first write a html site that works, then copy the html and return it in the HttpResponse
If I add src tag, like this: pastebin.com/uKduU9L3, it suddenly stops working.
it seems you need some training in javascript. try it on an empty html page before trying it in django. tip: you need two script tags.
It seems like I've approchead my problem from the wrong perspective. I've accepted you're answer and sincerily thank you for the help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.