1

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")
Community
  • 1
  • 1
Rajat Vij
  • 689
  • 1
  • 6
  • 12
  • whats your question ... your on click calls your js function ... which loads a url, that url loads your view function ... – Joran Beasley Oct 12 '15 at 17:56
  • My view function is not getting called. I am trying to call it through the ajax part referring the link i have given in my post. But i think the ajax part is not working here – Rajat Vij Oct 12 '15 at 19:47

0 Answers0