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

396
Views
How to pass variable from view.py to javascript without using render() in django

return render(request, 'index.html', {"flag": flag, "form": form})

I want to pass flag value which should be read by javascript to change style of an element in template, but since rendering it again the flag value is lost and set back to "none" ,is there any way to not use render and pass the variable to a url where the page is already rendered

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

In your javascript file use ajax method to call your views.py, 

   $.ajax({
            type: "GET", method GET or POST
            url: "/url_name/", //url of site no need to enter full site

            data: {
                'key': value, // data you want to pass if any
            },

            contentType: 'application/json',

            success: function (response) {
                  console.log(response.flag) // Gives value of flag  
            },

            error: function (data) {
                console.log('error');
            }
        });;

In your views.py you can pass your flag value as JSON response,

    def view_name(response):
        return JsonResponse({'flag': flag }, content_type='application/json')
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!