'Acces AJAX data as form data from a dictionary in Django

I am beginner in Django. I am trying to save a form data through AJAX. my code is

$("#form").submit(function (e) {
        e.preventDefault();
        var serializedData = $(this).serialize();
        value = "user1";
        
        $.ajax({
            type: 'POST',
            url: "{% url 'create-form' %}",
            data: { "serializedData":serializedData,
            "value":value,
            'csrfmiddlewaretoken': '{{ csrf_token }}',
            },
            success: function (response) {
                console.log("response", response);
                /*window.location.replace("https://www.youtube.com/"); */
            },
            error: function (response) {
                console.log("error:", response.responseJSON.error);
                
            }
        })
    })

And i am trying to accept data in views like this... I used class based view. I added only needed part of views.py code.

def post(self, *args, **kwargs):
    if self.request.is_ajax and self.request.method == "POST" :
            form = self.form_class(self.request.POST)
            val = self.request.POST.get("value")
           

            if val == "user1" :
                
                if form.is_valid():
                    print("form is valid ")
                    return JsonResponse({"Sucess": True}, status=200)
                else:
                    print("form is not valid ")
                    return JsonResponse({"error": form.errors} , status=400)

.. form_class is my form name.My problem is when i used to save form = self.form_class(self.request.POST).. It access all values that i sended through AJAX. I want only save serialized data in form. I want to save it as form data because i want to work form with is_valid.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source