Best way to handle POST request from external scripts

Hi Everyone,

I have this simple use case: Every time someone calls for a POST request to my URL, I want to change the interface of my website in some way (let’s say to display the content of the post request when its a bunch of strings). I understand how to hand POST requests in the Django backend but unsure of how to “trigger” a function in JS once a POST request is called.

Currently, I have a js function that is set on a timer which uses a GET to check the database that the POST request updates, it then changes based on the value of the database. Is there a better way of doing this?

I’m thinking: What if the django backend returns a render of the html page anytime it gets the specific POST request. Would this affect all users looking at the page if the POST request is made and django renders a HTML?

Thanks in advance!!

What I think you’re asking:

Person A submits a POST to update the database.
Django processes that post, and then notifies People B, C, and D that the database has been updated.

If that’s the situation you’re describing, please confirm.

And if my understanding is correct, then no, what you’re looking to do is not possible without some JavaScript and possibly additional tooling on the server side. (I can explain further if necessary.)

If I’m not understanding what you’re asking for, then please explain.

1 Like

Hi Ken!

Yes, that is exactly what I’m trying to do. Basically the exact flow is as follows:

Front-end button clicked => Script runs in background via celery => Script updates database via POST => **Front end reads database and updates UI according to database values **

Currently I’m simply defining a JS function to run every 3 seconds that checks the database for changes and then updates the UI. Wondering if it’s possible to have a better solution whereby as soon as the scrip updates the database, somehow the UI will start to update.

Totally open to JS and tools. Although a purely JS/Django solution is preferred.

Thanks,

J

First, always keep in mind that in a standard HTTP request/response cycle, once the browser has received the response, it is no longer connected to the server. The server cannot push to those browsers because the browsers aren’t connected.

That’s the reason behind websockets. Websockets is a protocol allowing for a persistent connection to exist between browsers and a server. In the case of Django, this implies Channels.

There are other mechanisms as well - among other options, you have socket.io w/node.js, long-polling, short-polling (which is what you’re doing), and browser notifications.

But the end-result is going to be some degree of JS in the browser to take action to handle these events. The server cannot, by itself, update a page.

This is the exact guidance I’m looking for. Thanks again Ken!