Trigger a Javascript Alert from a Django backend event
I have a Django application that whe开发者_开发问答n submitting information, does some backend checks, and in a rare case that the checks fail, I need a Javascript Alert to be displayed. How do I trigger the JS event from the Django backend?
I need this done from the backend - it can't be triggered by any html or 'onchange' event etc.
Here's how it works: User submits information and a background process in backend begins - the current page is updated/refreshed to indicate this has occurred. While the checks are being done, the user is free to roam anywhere, or choose to remain on the same page. Once the checks are done, I need code that can trigger the JS event that gets the alert message shown.
In one of the django projects I've worked on they used jQuery's ajax to do some of the form submissions. If not using jQuery, I had read an ajax tutorial a little ways back that gave me a little bit of insight. I think if you were to post *the data from your page using an ajax call, and then could utilize the ajax call back function to trigger an alert.
I'm not sure how this would work for your multi-page situation, the cases I saw all had the intent of keeping the user on the page or let them leave without having to warn them of any issues. If I had to guess, I would think that so long as javascript files involved are loaded on any of the pages the user could possibly go to, that it wouldn't matter where they currently are.
Let me clarify your requirement:
You have a step in your process where you want to do some background processing that can take some time. You'd like the user to go the following page, but then alert the user via some 'Alert' mechanism.
How about this: Take the user onto the following page, and have a javascript function that is bound to the onLoad event of the following page(s). The JavaScript function queries the backend that is monitoring the progress of the backend checks you are doing, and in case of a positive response trigger the alert.
精彩评论