0

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

1 Answer 1

2

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>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.