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
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')