I have been trying to call a function in my Class Based View from an onclick event using javascript. Please check my code below and let me know how i should approach the issue as i have been fairly new to python.
I followed following post : Django: How can I call a view function from template?
What i am really trying to do here is to dump the json data that i am getting in my javascript to a worker function in my django view. For that i followed this approach, i would appreciate if someone can help me with this, or if there is any norm or django way of doing this, then i would appreciate if someone can help me direct that way.
I will apologize in advance as i have started learning python since last may so it may be possible that i might doing some noob mistake here.
Javascript function
function request_access(){
var message = new Object();
message.platform_token = document.getElementById("id_platform_token").value;
message.platform_ui = document.getElementById("id_platform_uid").value;
var request_data= JSON.stringify(message);
alert(request_data);
$.ajax({
type: 'POST',
url: 'ui/seeds/<pk>/update',
data : { 'request_data': request_data},
success : function(json) {
$("#request-access").hide();
alert("requested access complete");
alert(data);
}
})
}
HTML button
<form action="" method="post">{% csrf_token %}
{{ form|crispy }}
<input type="submit" value="Update" onclick="request_access(this)" id="{{ data }}"/>
Django view under my Class based view
def request_access(self, request):
print("django view function")
m = request.POST.get('request_data')
z = request.POST.get('platform_uid')
print z
self.rabbit_worker(json.dumps(m))
return HttpResponse(json.dumps(m), content_type="application/json")