Srikrishnan Suresh
Srikrishnan Suresh

Reputation: 729

Django - retrieve form data

I am trying to send a form through ajax call to django view as follows:

    $.ajax({
            type: 'POST',
            url: "{% url 'edit_experience' %}",
            data: {
                  item_id : itemId,
                  form: $("#sample-form").serialize()
            },
            beforeSend : function() {

            },
            success: function (data) {

            },
            error: function(data) {

            },
            complete : function() {

            }
});

I am able to see the data being posted to the http server:

form    role=Fresher&organization=TestOrganization&description=Hard+work+pays
item_id 3

My problem is in the server side where I am trying to fetch the data. I am able to access the item_id but I am having a problem accessing the form elements:

In Django View:

def edit_experience(request):
     request.POST['form']['role']
     return ....

This does not fetch the role. What is the correct way to fetch all the form attributes??

Kindly help. Thanks in advance.

Upvotes: 0

Views: 76

Answers (1)

catavaran
catavaran

Reputation: 45555

To fetch attributes from the querystring you can use QueryDict:

from django.http import QueryDict
d = QueryDict(request.POST['form'])
role = d['role']

Upvotes: 1

Related Questions