Question: I'm trying to auto-submit the form when the file is selected. I know this question had already been asked on StackOverflow but that didn't solve my problem

```
<form id="img-compressor-form">
<input type="file" class="hide" id="img-upload" name="img-upload" onchange="form.submit()"> // Hidden 
<button name="img-upload-btn" onclick="document.getElementById('img-upload').click();" id="img-upload-btn" class="btn btn-primary">Compress</button>
</form>

```

ajax

```
        <script>
            $(document).ready(function(){
              $("#img-compressor-form").on("submit", function(e){
              e.preventDefault()
              var formData = new FormData(this);

              $.ajax({
                 url: "assets/script/upload.php", 
                 type: 'POST',
                 data: formData,
                 contentType: false,
                 processData: false,
                 success: function(data){
                 }
              });

         });
      });
        </script>

```

If I'm not wrong, it is not triggering an ajax call after auto-submit the form.

So what I'm exactly trying to do is trigger the ajax call after auto-submit

What am I doing wrong?

Update:- I tried all of the methods that have been suggested below but none of them worked. everything works fine If I submit the file through the input(file) which I want to keep hidden. It didn't work If I submit the file through the button (which would eventually trigger the input(file) as you can see the onclick event in my code). it didn't do anything after submitting the file through the button.

It worked when I placed this code in the separate js file

```

$('#img-upload').change(function() {
  $('#img-compressor-form').submit();
});

```

Thank you

Answer: 
Django's `TestCase` uses `django.test.Client` as the client and it does not set the content type etc. according to the `format` kwarg, instead it uses the `content_type` kwarg so you should either set that or use `APITestCase` from DRF which will use the `APIClient` class.

While using `TestCase`:

```
def test_search_with_full_name(self):
    data = {'class_id': '1', 'filter_options': {'full_name': 'user 1'}}
    response = self.client.post('/api/students', data, content_type='application/json')

    self.assertEqual(response.status_code, 200)

```

While using `APITestCase` from DRF:

```
from rest_framework.test import APITestCase

class YourTest(APITestCase):
    def test_search_with_full_name(self):
        data = {'class_id': '1', 'filter_options': {'full_name': 'user 1'}}
        response = self.client.post('/api/students', data, format='json')

        self.assertEqual(response.status_code, 200)

```
Could you change `format` to `content_type='application/json'`
[Django docs say this](https://docs.djangoproject.com/en/2.2/topics/testing/tools/)

> 
> If you provide content\_type as application/json, the data is serialized using json.dumps() if it’s a dict, list, or tuple. Serialization is performed with DjangoJSONEncoder by default, and can be overridden by providing a json\_encoder argument to Client. This serialization also happens for put(), patch(), and delete() requests.
> 
> 
> 

---

There might be an issue with serializing the dictionaries as values for POST fields. So in case if above solution doesn't work another way could be a serialize filter options by your self using `json.dumps(filter_options)` before passing to the request. And then read them via `json.loads(request.POST.get(filter_options))` in your view.