How to send the formdata from the ajax to Django view in PUT request?


I am sending the ajax call to the python django view(no rest framework) and want to perform the update operation in django using PUT request.

This update operation involved files too so I don’t know to do PUT operation for files.

I have performed GET, POST, DELETE operations but not understanding the PUT.

I am sending the formdata object from the ajax. Thanks in advance!!


<!DOCTYPE html>
<html lang="en">
  <title>Bootstrap Example</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="">
  <script src=""></script>
  <script src=""></script>
  <script src=""></script>

<div class="container">
  <form action="" id="submitassignment" method="post" role="form" enctype="multipart/form-data">
    <div class="form-group">
        <input type="hidden" value="{{ }}" id="id">
      <label for="email">Name:</label>
      <input type="text" class="form-control" id="name" placeholder="Enter email" name="name" value="{{ }}">
    <div class="form-group">
      <label for="pwd">age:</label>
      <input type="number" class="form-control" id="age" placeholder="Enter password" name="age" value="{{ obj.age }}">
      <div class="form-group">
      <label for="pwd">profile_photo:</label>
      <input type="file" class="form-control" id="profile_photo" placeholder="Enter password" name="profile_photo" value="{{ obj.profile_photo.url }}">
    <div class="form-group form-check">
      <label class="form-check-label">
        <input class="form-check-input" type="checkbox" name="remember"> Remember me
    <button class="btn btn-success btn-sm" type="submit" form="submitassignment" id="assignment_submit_button">Submit</button>

        $(document).ready(function () {
        $("#submitassignment").submit(function (e) {
            var formdata = new FormData();
            formdata.append("profile_photo", $("#profile_photo")[0].files[0])
            formdata.append("name", $("#name").val())
            formdata.append("age", $("#age").val())
            formdata.append("id", $("#id").val())

                url: "/student/",
                method: "PUT",
                async: false,
                cache: false,
                contentType: false,
                enctype: 'multipart/form-data',
                processData: false,
                data: formdata,
                success: function (response) {
def student_operation(request):
    if request.method == "GET":
        obj = Student.objects.all()
        return render(request, "datatables.html", {"obj": obj})
    elif request.method == "POST":
        name = request.POST.get("name")
        age = request.POST.get("age")
        profile_photo = request.FILES.get("profile_photo")
        obj = Student(name=name, age=age, profile_photo=profile_photo)
        return HttpResponse("success")
    elif request.method == "DELETE":
        data = json.loads(request.body)
        id = data.get('id')
        obj = Student.objects.get(id=id)
        return HttpResponse("success")
    elif request.method == "PUT":
        # Need to do code here