Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

199
Views
Passing a variable to python using Ajax method

I want to pass a variable using Ajax from a JS function to a python function in my Django project. I am able to call the python function. Using the ajax method, I know this because the print function in the python function runs. However how to I pass my variable through this ajax call? I just want to pass the local variable so it can be printed by the python function.

JS

function calling (){
    var local = 25
    
    $.ajax({
        type:'GET',
        url:'printsome/',
        success: function(response){
            console.log('success');
            console.log(local);
        },
        error: function (response){
            console.log('error');
        }
    });

}

python

def print_some(request):
    from django.http import JsonResponse
    print('this python function was called')
    return JsonResponse({})
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

For example, you can pass the variable as a queryparameter:

function calling (){
    var local = 25
    
    $.ajax({
        type:'GET',
        url:`printsome/?variable=${local}`, // <- Add the queryparameter here
        success: function(response){
            console.log('success');
            console.log(local);
        },
        error: function (response){
            console.log('error');
        }
    });

In django you can access the queryparameter like the following:

from django.http import JsonResponse
def print_some(request, ):
    variable = request.GET.get('variable', 'default')
    print('Variable:', variable)
    return JsonResponse({})

Read more about it here: HttpRequest.GET

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!