Pass a string variable from javascript file to a python function

Viewed 87

I have an ag-grid. Clicking on its context menu is opening a modal that has a drop-down menu that I want to populate dynamically by fetching the data from a SQLite database.

I want to pass a string variable - network_num from my .js file to a python function. I will then use this variable to execute a query to fetch data from the database. I should then send the fetched data in JSON format to .js file to populate the drop down menu. I am using ajax to send the variable, but it is not working. Please help.

Here is the code

 $.ajax({
                    url: "/get_activity",
                    type: "POST",
                    dataType: "json",
                    data: {
                        network_num: network_num,
                        csrfmiddlewaretoken: '{{ csrf_token }}'
                        },
                    success : function(json) {
                        alert("Successfully sent the variable");
                    },
                    error : function(xhr,errmsg,err) {
                        alert("Could not send URL to Django. Error: " + xhr.status + ": " + xhr.responseText);
                    }
                });
def get_activity(request):
    print("Get activity function is running")
    #I have my sql query here that I want to execute
1 Answers

Assuming you already have the infrastructure built, in python, request should be passed as a dictionary object, so unless the caller is doing something crazy, it should just be request['network_num']

Related