Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

392
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda