0

I have a booleanField in my hostelApplication model, I want to change that field using html button, but I don't know much about js. Please anyone tell me what I am doing wrong. here is my models.py, I have shown only necessary fieds.

class hostelApplication(models.Model):
    hostelName = models.CharField(verbose_name='Select Hostel', max_length=20, choices=hostels, default='shivalik')
    isApproved = models.BooleanField(default=False)

    def __str__(self):
        return self.hostelName

I have added my url path in urls as

url(r'^ajax/change_status/$', views.ajax_change_status, name='ajax_change_status')

here is my html file

{% for application in applications %}
    {% csrf_token %}
    <div class="table-content">
        <div class="table-row">
        <div class="table-data">{{application.hostelName}}</div>
        <div class="table-data">{{application.dateApplied}}</div>
        <div class="table-data">
            <button type="button" class="btn btn-info" id="state">Approve</button>
        </div>
    </div>
{% endfor %}

I have added this js script in this template on the top

<script>
     $("#state").on('click', function () {
     var username = $(this).val();
     var isApproved = true // or false, you have to set it
     var id = application.id // you have to set it
     $.ajax({
        url: '/ajax/change_status/',
        data: {
              'csrfmiddlewaretoken': $('input[name="csrfmiddlewaretoken"]').val(),
              'isApproved': isApproved
              'id': username
        },
        dataType: 'json',
        success: function (data) {
        if (data.success) {
        alert("ajax call success.");
        // here you update the HTML to change the active to innactive
        }else{
            alert("ajax call not success.");
         }
      }
  });

});
</script>

2 Answers2

0

some errors with js syntax. every line need end up a semicolon, and every item of a object(data={}) need end up with a comma.

Blackdoor
  • 922
  • 1
  • 6
  • 12
0
$("#btn").click(function(){

        $.ajax(
            {
                type:"POST",
                url: "/load_layout/",
                data:{
                # Some data u want to sand               
                },
                success: function( respData ) 
                {
                    alert("Sent to python");
                },
                error: function (jqXHR, status, err) {
                    alert("Local error callback.");
                    return "error";
                  }
             });
})

in ur views.py

def load_layout(request):
 data = request.POST['data_name_give in_ajax_req']
 user1 = database_object.object.get(name=data)
 user1.name = xyz
 user1.save()
 return some templete

if this doesnt work call me on kiranu941@gmail.com

Uday Kiran
  • 229
  • 2
  • 13