user5443928
user5443928

Reputation:

Getting ValueError while validating the form fields using Django and Javascript

I am getting the below error while validating the field using Javascript and Django.

Error:

ValueError at /insert/
invalid literal for int() with base 10: ''
Request Method: POST
Request URL:    http://127.0.0.1:8000/insert/
Django Version: 1.11.2
Exception Type: ValueError
Exception Value:    
invalid literal for int() with base 10: ''
Exception Location: /usr/local/lib/python2.7/dist-packages/django/db/models/fields/__init__.py in get_prep_value, line 1853
Python Executable:  /usr/bin/python
Python Version: 2.7.6

I am explaining my code below.

insert.html:

<script type="text/javascript">
    function validateForm(){
        var s=document.frmfeed;
        if(s.name==''){
            alert('Please enter the name');
            return;
        }else if(s.phone==''){
            alert('Please enter the phone no');
            return;
        }else if(s.age==''){
            alert('Please enter the age');
            return;
        }else{

        }
    }
</script>

<form method="post" action=" " onSubmit="return validateForm();" name="frmfeed">
    {% csrf_token %}
    <label>Name: </label>
    <input name="name" value="{{person.name}}">
    <br>
    <label>Phone: </label>
    <input name="phone" value="{{person.phone}}">
    <br>
    <label>Age: </label>
    <input name="age" value="{{person.age}}">
    <br>
    <input type="hidden" name="id" value="{{person.id}}">
    <input type="submit" value="Submit">
</form>

view.py:

def insert(request): 
    # If this is a post request we insert the person 
    if request.method == 'POST': 
        p = Person( 
        name=request.POST['name'], 
        phone=request.POST['phone'], 
        age=request.POST['age'] 
        ) 
        p.save() 
        return redirect('/') 
    else: 
        return render(request, 'insert.html', {})

model.py:

class Person(models.Model):
    name = models.CharField(max_length=200)
    phone = models.CharField(max_length=15)
    age = models.IntegerField()

Here I need to check all input fields those should not be blank before submit but when I am clicking on submit button those error are coming. Please help me to resolve this error.

Upvotes: 1

Views: 157

Answers (2)

zaidfazil
zaidfazil

Reputation: 9235

Your model contains an Integer field for age, you are passing a string to the field. request.POST['age'] always returns a string.

Try your view edited like this,

def insert(request): 
    if request.method == 'POST': 
        p = Person( name=request.POST['name'], phone=int(request.POST['phone']), age=int(request.POST['age']) )
        p.save() 
        return redirect('/') 
    else: 
        return render(request, 'insert.html', {})

Here I need to check all input fields those should

NOTE: Assuming yourphone field is also an Integer field. If not ignore the changes in the phone= field.

EDIT

You could specify type= "number" , in input tag of age in the form.

<form method="post" action=" " onSubmit="return validateForm();" name="frmfeed"> 
{% csrf_token %} 
<label>Name: </label> 
<input name="name" value="{{person.name}}" required> <br> 
<label>Phone: </label>
<input name="phone" value="{{person.phone}}" required> <br> 
<label>Age: </label> 
<input type="number" name="age" value="{{person.age}}" required> <br> 
<input type="hidden" name="id" value="{{person.id}}"> 
<input type="submit" value="Submit"> 
</form>

Upvotes: 0

Daniel Roseman
Daniel Roseman

Reputation: 599788

Your JS is not actually doing any validation, in that it is not preventing submission if the values are blank. You need to return false in those cases.

However, you should not do any of this. Modern browsers include form validation already, which will kick in if you add a required attribute on the fields.

Django itself will output that attribute - and, just as important, also validate on the server side - if you use a form class. You should definitely be doing this.

Upvotes: 1

Related Questions