0

I have a model here

class personaldetails(models.Model):
    fname=models.CharField(max_length=30)
    lname=models.CharField(max_length=30)
    email=models.CharField(max_length=30)
    mbleno=models.IntegerField()
    maritalstatus=models.CharField(max_length=30)
    currentindustry=models.CharField(max_length=30)
    functionalarea=models.CharField(max_length=30)
    annualsalary=models.CharField(max_length=30)

views.py

def personalinformation(request):
    print "hello"
    if request.method=='POST':
        print "hi"
        fn=request.POST.get('fname')
        print fn
        ln=request.POST.getlist('lname')
        email=request.POST.getlist('email')
        mno=request.POST.getlist('mbleno')
        ms=request.POST.getlist('maritalstatus')
        ci=request.POST.getlist('currentindustry')
        fc=request.POST.getlist('functionalarea')
        ans=request.POST.getlist('annualsalary')
        personaldetails(fname=fn,lname=ln,email=email,mbleno=mno,maritalstatus=ms,currentindustry=ci,functionalarea=fc,annualsalary=ans).save()
        s="example"
        return HttpResponse(s,mimetype='application/json')
    return render(request,"itechdisplay.html")      

my itechdisplay.html

<html>
<head>
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
    $('#formid').submit(function(){
    $.ajax({
         type:$(this).attr('method'),
        data:$(this).serialize(),
        url:'/personalinformation/',
        success:function(data) {
        alert($(data));
}
  });
    });
});
</script>
</head>
<body>
<form action="." id="formid" method="post">{% csrf_token %}

Firstname:<input type="text" id="firstname" name="input1"><br>
Lastname:<input type="text" id="lastname" name="input2"><br>
e-mail:<input type="text" id="email" name="input4"><br>
mobileno:<input type="text" id="mobleno" name="input5"><br>
Maital status:<select id="maritalstatus">
<option value="s1" selected>single</option>
<option value="s1">married</option>
<option value="s1">divorsed</option>
<option value="s1">other</option>
</select><br>
Current industry<select id="currentindustry">
<option value="s1">IT-software/software sevices</option>
<option value="s1" selected>it-hardware/networking</option>
<option value="s1">insurance</option>
<option value="s1">other</option>
</select><br>
Functional area:<select id="functionalarea">
<option value="s1" selected>IT-software</option>
<option value="s1">it-hardware</option>
<option value="s1">agent</option>
<option value="s1">other</option>
</select><br>
Annual salary<select id="annualsalary">
<optgroup label="Hour wise">
<option value="s1">less than 40$/hr</option>
<option value="s1">40-50$/hr</option>
<option value="s1">50-60$/hr</option>
<option value="s1">above 60$</option>
</optgroup>
<optgroup label="month wise">
<option value="s1">less than 20,000$</option>
<option value="s1">20-30,000$</option>
<option value="s1">30-40,0000$</option>
<option value="s1">above 40,000$</option>
</optgroup>
</select><br>
<input type="submit" id="save1" value="save">
</form>
</body>
</html>

I have written a model. In my template I want to use post method. When I submit a form I want the details to be moved to views and store it in the database and retrieve it to be displayed. I don't know know how to write the views to bring the data from template. I tried as I mentioned above. But I get this error as follows

int() argument must be a string or a number, not 'list'

in a following line of my views.py

personaldetails(fname=fn,lname=ln,email=email,mbleno=mno,maritalstatus=ms,currentindustry=ci,functionalarea=fc,annualsalary=ans).save()

How to write the view? Any approaches can be appreciated.. thanks in advance

1 Answer 1

1

A simple version not utilizing your models (I'll leave that up to you as an exercise)

def my_ajax_view(request):
    if request.is_ajax():
        if request.method == 'POST':
            //do you logic here
            response_data = {'success': 'weee'}
            return HttpResponse(json.dumps(response_data), content_type="application/json")
        else:
            return HttpResponseForbidden()
    return HttpResponseForbidden() 

This is how you write a normal function ajax view. Now to the errors that you're having. Firstly I would recommend you not naming your variables mc, mmo, ans, ci or func, basically this makes it 100 times harder for anyone (even you cause you'll forget in a day what you wrote) to debug.

Secondly you're using getlist() on the POST. This will give you a list containing the information and when you try to save it your IntegerField mbleno is expecting an integer or string instead of a list.

I would try to switch

mno=request.POST.getlist('mbleno')

to

mno=request.POST.get('mbleno')

which most likely will give you other errors, but it's a good start!

Sign up to request clarification or add additional context in comments.

3 Comments

it is not getting entered into request.is_ajax()
yes I have changed it like you said.. But it is not bringing the values from templates to views. I shows the error-(1048, "Column 'fname' cannot be null") in my views(first line after if loop)
Thanks a lot.. Am getting

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.