Firas252 Firas252 - 1 year ago 152
Javascript Question

Django using functions from a python class from frontend?

I am new to Django, but i managed to create the back-end and front end for my website but in the front end i am connecting to an external socket and getting data on the fly and i implemented a class that has the function

that adds the element to my graph database

How can I call this function from the front end so it is applied in the back-end without disturbing the rendering of the website.

this is the js code on the front-end page

eventToListenTo = 'tx'
room = 'inv'

var socket = io("");
socket.on('connect', function() {
// Join the room.
socket.emit('subscribe', room);

socket.on(eventToListenTo, function(data) {


also after getting the Data i am viewing it in to the user with ability to check the details of this data so it should be added to the graph before viewing it

Answer Source

You can start providing an api point to your Django app that will take parameters from the request body (or/and request parameters) and call your function. So create an url like /api/add2grah. And you call it in the front end with a classic async call.

Now if your function takes a long time, you may want to start using a task queue, so than your api function returns immediately but your add2Graph runs in the back. Good and simple solutions are Huey, Django-q or django-rq (they're simpler than Celery).

Does that answer something ?

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download