gauravhalbe
gauravhalbe

Reputation: 402

django: Pass specific form data through JQuery

How to pass any specific form value for processing, through Jquery in Django? I want to pass the value of label 'machine' in my form to views.py by JQuery. Here is my code:

template.py:

<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script type="text/javascript">

$(document).ready(
                 function() {  

                    $("#submit").click(function (event) {
                        event.preventDefault();
                        var machine = $("#machine").val();    //taking the machine value
                        var data = { machineID: machine};     //data dictionary
                        var url = "/vsawebauto/automation/results/job_processing";
                        $.getJSON(url, data, function(machines) {
                                $("#progress").text(machines[0].fields['machine_name']);
                        });                            
                      });                           
});
</script>
</head>

<body>
<form name="resultsForm" method="post">
{% csrf_token %}

<br><label id="machine" value="{{ selected_machine }}">Selected Machine: {{ selected_machine }}</label></br>
<br><input type="submit" id="submit" value="Submit Job" /></br>
<br><div class="progress" id="progress"></div></br>
</form>
</body>
</html>

views.py:

def job_processing(request):
    machineID = request.POST.get('machineID', False)
    machine = Client.objects.get(pk=machineID)
    isAutomationPkgCopySuccessful = JobRunner.copyAutomationPackage(machine.machine_name,machine.username,machine.password,request)
    availableMachines = runningProcess.ParseMachineList(request)
    json_models = serializers.serialize("json", availableMachines)
    return HttpResponse(json_models, mimetype="application/javascript; charset=UTF-8") 

When I debug the code, machineID in views.py has boolean value 'False' (it should have the value of $("#machine").val() passed from templates.py which is '1')

Upvotes: 0

Views: 1627

Answers (1)

user710907
user710907

Reputation: 772

For starters you are using $.getJSON and then looking for machineID in request.POST

you should use request.GET.get('machineID', False)

Secondly I would imagine your form will get submitted via POST always there is no point attaching a click event on the submit button.

<script type="text/javascript">
$(document).ready(function() {
$("#myform").submit(function(event){
    event.preventDefault(); 
    var machine = $("#machine").val();
    var data = { machineID: machine};
    var url = "/vsawebauto/automation/results/job_processing";
    $.getJSON(url, data, function(machines) {
       $("#progress").text(machines[0].fields['machine_name']);
    });  
});
</script>

<form id="myform">
  <br><label id="machine" value="{{ selected_machine }}">Selected Machine: {{ selected_machine }}</label></br>
  <br><input type="submit" name="submit" value="Submit"> < /br>
  <br><div class="progress" id="progress"></div></br>
</form>

Upvotes: 2

Related Questions